日期:2014-05-20  浏览次数:20899 次

【PK贴】大家晒下C#开发代码的小技巧啊!
推荐区看到《大家晒下java开发代码的小技巧啊!》
突发奇想,搞个C#版的和它PK下。
抛砖引玉:

1. 使用as,而非is
C# code

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



2. 使用DataReader读取数据
C# code

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



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

using(SqlDataReader reader = SqlHelper.ExecuteReader(cmd)) {
  Ilist<EmployeeInfo> list = new List<EmployeeInfo>();
  while(reader.read()) {
    list.add(new EmployeeInfo(
      reader.getInt32(0)
      // 其它字段
 ));
  }
}



4. 使用StringBuilder操作频繁变动的字符串,但以下情况例外
C# code

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



------解决方案--------------------
。。。
------解决方案--------------------
刚给control写了个扩展方法,按回车发送tab键的 ^_^
C# code

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

------解决方案--------------------
占座
------解决方案--------------------
遍歷control
C# code
 private void PanelClear(Control c)
        {
            foreach (Control cc in c.Controls)
            {
                if (cc.GetType() != typeof(Panel))
                {
                    PanelClear(cc);
                }
                else
                {
                    ((Panel)cc).Visible = false;
                }
            }
        }

------解决方案--------------------
1)对所有类都重写ToString()方法,这样在调试,绑定中都非常有用。
2)使用Enum代替奇迹数

先说两点。想起来再加。

------解决方案--------------------
找出是否存在某個窗體FORM
C# code
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"]

------解决方案--------------------
插队
------解决方案--------------------
lz写的第4点,用String.Format() 如何?

C# code

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

------解决方案--------------------
留个名,学习
------解决方案--------------------
將datagridview的某個checkbox不顯示
C# code
  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;
        }