20条.net【C#】编码良好习惯

2010年1月17日 | 标签:

为了提高代码质量,经过搜索整理出以下20条,望共同进步!!!

  1. 不要硬编string/ numeric,可以使用一些常量,例如:
  2. 不好的习惯:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    static void Main(string[] args)
            {
                int Count;
                Count = 100;
                if (Count == 0)
                {
                    // 执行一些操作
                }
            }

    好点的习惯:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    private static const int ZERO = 0;
            static void Main(string[] args)
            {
     
                int Count;
                Count = 100;
                if (Count == ZERO)
                {
                    // 执行一些操作
     
                }
     
            }
  3. 对于字符串比较-使用String. Empty ,而不是”"
  4. 默认情况下将成员变量定义为 ‘private’,如果有需要,将其扩展为protected 、public 、 internal。
  5. 默认情况下使私人范围的优点是,在XMLSerilaization期间,默认情况下它会序列化的所有public成员。

  6. 当我们要在循环操作字符串,使用StringBuilder,而不是字符串。
  7. 示例如下。
    不好的习惯:

    1
    2
    3
    4
    5
    
    String  temp = String.Empty;
                for( int i = 0 ; i<= 100; i++)
                {
                    temp += i.ToString();
                }

    好点的习惯:

    1
    2
    3
    4
    5
    
    StringBuilder sb = new StringBuilder();
                for ( int i = 0 ; i<= 100; i++)
                {
                    sb.Append(i.ToString());
                }
  8. 简单的操作,比起Collection更倾向使用Array
  9. 比起ArrayList更倾向使用Generic Collection
  10. 比起HashTable更倾向使用Generic Dictionary
  11. 对于字符串的操作和存储,倾向与StringCollection和StringDictionary
  12. 使用适合的数据类型。
  13. 例如:你想要判断状态,使用bool比int要好。
    不好的习惯:

    1
    2
    3
    4
    5
    6
    
    int Check = 0;
                if( Check == 0 )
                {
                    // 执行一些操作
     
                }

    好点的习惯:

    1
    2
    3
    4
    5
    
    bool Check = false;
                if(!Check)
                {
                    // 执行一些操作
                }
  14.   使用as做类型转换的时候,对转换后的值进行null值判断
  15. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    class A
       {
     
       }
       class B : A
       {
     
       }
        B objB = new B();
        A objA1  = (A) objB;
        A objA2 = objB as A;
        if( objA2 != null)
        {
           //执行所需的操作
         }
  16.   创建wcf代理,使用using表达式
  17. 1
    2
    3
    4
    
    using(Cerate the proxy)
    {
          //执行所需的操作
    }
  18.   对于昂贵的资源(例如Connection, File 等等),遵照’Acquire late, release early’ (尽量晚的获取,尽量早的释放)准则。
  19. 例子:如果你想在数据操作时,使用的SqlConnection对象,请在方法级别,而不是在类级别创建实例。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    class MyData
    {
        public MyData()
        {
        }
        public List GetAllCustomer()
        {
             using (SqlConnection objConnection = new SqlConnection("Connection string"))
             {
                 //执行一些操作得到需要的数据
              }
         }
     }

    如果你想创建的类级别SqlConnection实例,确保您的类实现了IDisposable接口,并在Dispose()中清理SqlConnection实例。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    class MyData : IDisposable
    {
        SqlConnection objConnection ;
        public MyData()
        {
             objConnection = new SqlConnection("Connection string");
        }
        public List GetAllCustomer()
        {
             //通过objConnection得到需要的数据
         }
        public void Dispose()
        {
             //清理SqlConnection实例
              if( objConnection != null )
             {
                  if( objConnection.State == ConnectionState.Open)
                  {
                       objConnection.Close();
                  }
             }
         }
     }
  20.   如果你不想别人扩展你的类功能,使用‘sealed’。
  21.   避免为每个类都声明‘destructor’ ,因为它会增加不需要常驻内存的类的生命周期。
  22.   相对manual threading,更倾向用Thread Pool 。
  23.   在循环内不要去调用其它方法。
  24. 例如:

    不好的习惯:

    1
    2
    3
    4
    
    for( int i = 0; i<= 100; i++)
    {
        Calculate(i);
    }

    好点的习惯:

    1
    2
    3
    4
    
    for( int i = 0; i<= 100; i++)
    {
        //直接写Calculate逻辑。
    }
  25.   不要在循环内处理异常,而是将循环处理的逻辑放在try/catch里面
  26. 不好的习惯:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    for(int i = 0 ; i<= 100; i++)
    {
       try
       {
       }
       catch(Exception ex)
       {
           throw ex;
       }
    }

    好点的习惯:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    try
    {
         for(int i = 0 ; i<= 100; i++)
         {
         }
    }
    catch(Exception ex)
    {
        throw ex;
    }
  27.   不用通过异常处理应用程序的逻辑
  28. 例如:

    不好的习惯:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    try
    {
        int x,y,z;
        x = 0;
        y = 10;
        z = y/x;
    }
    catch(DevideByZeroException ex)
    {
        Throw ex;
    }

    好点的习惯:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    try
    {
        int x,y,z;
        x = 0;
        y = 10;
        if( x != 0 )
        {
            z = y/x;
        }
    }
    catch(Exception ex)
    {
    }
  29.   相对 foreach,倾向使用for/while 循环
  30.   在层与层之间的交互,比起使用DataSet/DataTables更倾向于使用数据对象传递。
目前还没有任何评论.