zoukankan      html  css  js  c++  java
  • C#开发代码的小技巧1

    1. 使用as,而非is

    object o = GetFromCache("A_KEY");
    EmployeeInfo employee
    = o as EmployeeInfo;
    if(employee != null)
    {
    // TODO: 代码
    }

    2. 使用DataReader读取数据

    using(SqlDataReader reader = SqlHelper.ExecuteReader(cmd)) {
    while(reader.read()) {
    // TODO: 读取当前行的数据
    }
    }

    3. 尽量使用强类型集合(包括泛型集合),而非DataTable

    using(SqlDataReader reader = SqlHelper.ExecuteReader(cmd)) {
    Ilist
    <EmployeeInfo> list = new List<EmployeeInfo>();
    while(reader.read()) {
    list.add(
    new EmployeeInfo(
    reader.getInt32(
    0)
    // 其它字段
     ));
    }
    }
    4. 使用StringBuilder操作频繁变动的字符串,但以下情况例外

    代码一:string s = "str1" + "str2" + "str3" + "str4"; // 这段代码不需要使用StringBuilder,因为编译后的代码为 string s = "str1str2str3str4";

    代码二:String.Format("{0}{1}{2}{3}", str0, str1, str2, str3);

    5.control扩展方法,按回车发送tab键

    public static class ControlExtensions
    {
    public static void SendTabKey(this Control control, Keys key)
    {
    if (key == Keys.Enter)
    SendKeys.Send(
    "{TAB}");
    }
    }
    6.遍歷control

    private void PanelClear(Control c)
    {
    foreach (Control cc in c.Controls)
    {
    if (cc.GetType() != typeof(Panel))
    {
    PanelClear(cc);
    }
    else
    {
    ((Panel)cc).Visible
    = false;
    }
    }
    }
    7.对所有类都重写ToString()方法,这样在调试,绑定中都非常有用。
    8.使用Enum代替奇迹数

    9.找出是否存在某个窗体FORM

    for (int j = 0; j < Application.OpenForms.Count; j++)
    {
    if (Application.OpenForms[j].Name.Equals("FNO31000"))
    {
    fno3100
    = true;
    }
    }
    If (fno3100
    = true)
    FNO31000 fno
    = (FNO31000)Application.OpenForms["FNO31000"]
    10.将datagridview的某个checkbox不显示

    public void chang_COLOR(int i, string str)//將顯示為N者不顯示選取方塊
    {
    dataGridView1.Rows[i].Cells[str].ReadOnly
    = true;
    DataGridViewCell cell
    = new DataGridViewTextBoxCell();
    cell.Style.BackColor
    = Color.Wheat;
    //cell.ReadOnly = true;
    cell.Value = "N";
    cell.Style.BackColor
    = Color.White;
    dataGridView1.Rows[i].Cells[str]
    = cell;
    dataGridView1.Rows[i].Cells[str].Style.ForeColor
    = Color.White;
    dataGridView1.Rows[i].Cells[str].Style.SelectionBackColor
    = Color.White;
    dataGridView1.Rows[i].Cells[str].Style.SelectionForeColor
    = Color.White;
    }
    11.打開某個路徑下的程序

    Process p = new Process();
    p.StartInfo.FileName
    = "cmd.exe"; //设定程序名
    p.StartInfo.UseShellExecute = false; //关 ?Shell的使用
    p.StartInfo.RedirectStandardInput = true; //重定向标 ­ã输 ¤J
    p.StartInfo.RedirectStandardOutput = true; //重定向标 ­ã输 ¥X
    p.StartInfo.RedirectStandardError = true; //重定向错 ?输 ¥X
    p.StartInfo.CreateNoWindow = true; //设置不显 ¥Ü窗口
    p.StartInfo.WorkingDirectory = @"E:\";
    p.Start();
    //启 ?
    p.StandardInput.WriteLine("新增文字文件.bat");
    p.StandardInput.WriteLine(
    "exit");

    12.字符串留用技术
    字符串留用技术用来处理大量的字符串,而这些字符串中又会有许多重复的字符,例如:
    "str1", "str2", ... , "str100" 共10,000个这样的字符,毫无疑问,其实就99个字符
    这种情况可以使用到字符串留用技术,会提到性能
    13. 数组永远是0基的
    14. 使用多线程或异步操作的时候使用线程池的QueueUserWorkItem()方法将需要执行的任务排队,而不是手动去Start一个线程

    15.如果实现了IDisposable的对象在访问完成后要进行关闭,则在try{...}finally{...//关闭代码},或直接调用using(...){...}。

    16.在写公共类库,使用catch时,最好不要catch(Exception e){throw e;}而使用catch{throw;},同时如果使用catch捕捉异常,最好使用catch(最具体的Exception e){...}catch(最具体的Exception e){...}...一直到所有确认的错误都能检查到;而不要使用catch(基类Exception e){...}

    17.//尽量不用
    string str="";
    //而是
    string str=string.Empty;

    出处:http://blog.sina.com.cn/s/blog_6719966d0100i880.html

  • 相关阅读:
    Javascript中最常用的55个经典技巧(转)
    Yahoo!网站性能最佳体验的34条黄金守则(转载)
    你误解了Windows的文件后缀名吗?
    你不知道的Spring配置文件
    Thrift 个人实战--Thrift 的序列化机制
    Thrift 个人实战--Thrift 网络服务模型
    Thrift 个人实战--初次体验Thrift
    HBase 实战(2)--时间序列检索和面检索的应用场景实战
    Kafka实战系列--Kafka API使用体验
    Kafka实战系列--Kafka的安装/配置
  • 原文地址:https://www.cnblogs.com/movemoon/p/2756807.html
Copyright © 2011-2022 走看看