您的位置:首页 > 其它

20条.net编码习惯

2010-01-27 10:28 253 查看
来源:http://kb.cnblogs.com/page/55529/

1、不要硬编string/ numeric,可以使用一些常量代替。 (提高可读性)

int
 Count;

Count 
=
 
100
;

private
 
static
 
const
 
int
 ZERO  
=
  
0
;

if
(  Count  
==
  ZERO )

{

//
 执行一些操作

}

2、对于字符串比较-使用String. Empty ,而不是""。

3、不要声明成员变量为 public 或者proteted,尽量使用private 成员变量和public/protected 属性。 (修改)

4、当我们要在循环操作字符串,使用StringBuilder,而不是字符串,示例如下。

不好的习惯:

           String  temp 
=
 String.Empty;

            
for

int
 i 
=
 
0
 ; i
<=
 
100
; i
++
)

            {

                temp 
+=
 i.ToString();

            }

好点的习惯:

            StringBuilder sb 
=
 
new
 StringBuilder();

            
for
 ( 
int
 i 
=
 
0
 ; i
<=
 
100
; i
++
)

            {

                sb.Append(i.ToString());

            }

5、简单的操作,比起Collection更倾向使用Array。 (视情况,这里是建议)

6、比起ArrayList更倾向使用Generic Collection。 (视情况,这里是建议)

7、比起HashTable更倾向使用Generic Dictionary。 (视情况,这里是建议)

8、对于字符串的操作和存储,倾向于StringCollection和StringDictionary。 (视情况,这里是建议)

9、使用适合的数据类型。

    例如:你想要判断状态,使用bool比int要好。

不好的习惯:

           
int
 Check 
=
 
0
;

            
if
( Check 
==
 
0
 )

            {

                
//
 执行一些操作

            }

好点的习惯:

            
bool
 Check 
=
 
false
;

            
if
(
!
Check)

            {

                
//
 执行一些操作

            }

10、使用as做类型转换的时候,对转换后的值进行null值判断

   
class
 A

   {

   }

   
class
 B : A

   {

   }

    B objB 
=
 
new
 B();

    A objA1  
=
 (A) objB;

    A objA2 
=
 objB 
as
 A;

    
if
( objA2 
!=
 
null
)

    {

     
//执行所需的操作

    }

11、创建wcf代理,可以使用using表达式。 (很多地方可以这样使用)

           
using
(Cerate the proxy)

            {

                
//执行所需的操作

            }

 12、对于昂贵的资源(例如Connection, File 等等),遵照'Acquire late, release early’ (尽量晚的获取,尽量早的释放)准则。

例子:如果你想在数据操作时,使用的SqlConnection对象,请在方法级别,而不是在类级别创建实例。




代码

      
class
 MyData

        {

            
public
 MyData()

            {

            }

            
public
 List
<
Customer
>
 GetAllCustomer()

            {

               
using
 (SqlConnection objConnection 
=
 
new
 SqlConnection(
"
Connection string
"
))

               { 

                   
//
执行一些操作得到需要的数据

               }

            

            }

        }

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




代码

        
class
 MyData : IDisposable

        {

            SqlConnection objConnection ;

            
public
 MyData()

            { 

                objConnection 
=
 
new
 SqlConnection(
"
Connection string
"
);

            }

            
public
 List
<
Customer
>
 GetAllCustomer()

            { 

                
//通过objConnection
得到需要的数据

            }

            
public
 
void
 Dispose()

            {

                
//清理SqlConnection实例

                
if
( objConnection 
!=
 
null
 )

                {

                    
if
( objConnection.State 
==
 ConnectionState.Open)

                    {    

                       objConnection.Close();

                    }

                }

            }

        }

13、如果你不想别人扩展你的类功能,使用‘sealed’。

14、避免为每个类都声明‘destructor’ ,因为它会增加不需要常驻内存的类的生命周期。

15、相对manual threading,更倾向用Thread Pool 。

16、在循环内不要去调用其它方法。 (call function 有性能损耗)

例如:

不好的习惯:

for

int
 i 
=
 
0
; i
<=
 
100
; i
++
)

{    

   Calculate(i);

}

 好点的习惯:

for

int
 i 
=
 
0
; i
<=
 
100
; i
++
)

{

//
直接写Calculate逻辑。

}

17、不要在循环内处理异常,而是将循环处理的逻辑放在try/catch里面

不好的习惯:

for
(
int
 i 
=
 
0
 ; i
<=
 
100
; i
++
)

{

   try

   {

   }

   catch
(Exception ex)

   {

    throw
 ex;

   }

}

好点的习惯:

   try

   {

     for
(
int
 i 
=
 
0
 ; i
<=
 
100
; i
++
)

     {

     }

   }

   catch
(Exception ex)

   {

       
throw
 ex;

   }

 18、不用通过异常处理应用程序的逻辑

例如:

不好的习惯:

  try

  {

    int
 x,y,z;

    x 
=
 
0
;

    y 
=
 
10
;

    z 
=
 y
/
x;

   }

   catch
(DevideByZeroException ex)

   {

    Throw ex;

   }

好点的习惯:

try

 {

   int
 x,y,z;

   x 
=
 
0
;

   y 
=
 
10
;

   if
( x 
!=
 
0
 )

   {

      z 
=
 y
/
x;

   }

 }

 catch
(Exception ex)

 {

 }

19、相对for/while  ,倾向使用foreach循环。[更正]

20、使用多层架构的系统,层与层之间的交互,比起DataSet/DataTables更倾向于使用对象传递数据。

 

 

2010-01-27

目前我需要注意的是:第2、3、4、10、12、16和20条。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息