初学C#编程的注意事项

下面是8个C#编程时的注意事项是给初学者的,可能你知道,也可能你不知道,不过这些都是一些可能会让人疏忽的地方,还是要注意一下。

1.使用String变量:

考虑有下面的一个程序想判断一下字符串是否有内容。

 
1 if (someString.Length > 0)
2 {
3     // …
4 }

但是,这个字符串对象很可能是个空对象,所以,最好先判断一下null

 
1 if  (!String.IsNullOrEmpty(someString))
2 {
3     // 是不是更好一些?
4 }

2.字符器连接

 
1 string s = “dev”;
2 s += “-”;
3 s += “the”;
4 s += “-”;
5 s += “web”;
6 s += “.”;
7 s += “com”;

这样做没什么问题,只不过性能不高,因为+=操作符其实调用的是String类的Append访问,所以,+=会有两次函数调用,下面的则会好一些。

 
1 StringBuilder s = new StringBuilder();
2 s.Append(”dev”);
3 s.Append(”-”);
4 s.Append(”the”);
5 s.Append(”-”);
6 s.Append(”web”);
7 s.Append(”.”);
8 s.Append(”com”);

3.使用Console

 
1 Console.WriteLine("A= " + 1 + " B=" + 2 + " C= " + someValue);

和第二点说的一样,这并没有效率,使用下面的语句,会更有效率。

 
1 Console.WriteLine(”A: {0}\nB: {1}\nC: {2}”, 1, 2, someValue);

4.字符串转整型

 
1 int i = int.Parse(Request.QueryString["id"]);

这样做的问题是,如果有人这样请求你的页面:yourpage.aspx?id=A6,那么A6将会导致你的程序抛出一个异常。因为A6不是一个整数字符串。使用TryParse会好一点。

 
1 int i;
2 if (!int.TryParse(Request.QueryString["id"] , out i))
3 {
4     //…
5 }

5. 调用IDbConnection 的 Close 方法

 
01 IDbConnection dbConn = null;
02   
03 try
04 {
05     dbConn = new SqlConnection(”some Connection String”);
06     dbConn.Open();
07 }
08 finally
09 {
10     dbConn.Close();
11 }

调用SqlConnection的构造函数可能会出现一个异常,如果是这样的话,我们还需要调用Close方法吗?

 
01 IDbConnection dbConn = null;
02   
03 try
04 {
05     dbConn = new SqlConnection(”Some Connection String”);
06     dbConn.Open();
07 }
08 finally
09 {
10     if (dbConn != null)
11     {
12         dbConn.Close();
13     }
14 }

6.使用List类

 
1 public void SomeMethod(List<SomeItem> items)
2 {
3     foreach(var item in items)
4     {
5         // do something with the item…
6     }
7 }

如果我们只是遍历List容器中的所有内容的话,那么,使用IEnumerable接口会更好一些。因为函数参数传递一个List对象要比一个IEnumerable接口要花费更多的开销。

 
1 public void SomeMethod(IEnumerable<SomeItem> items)
2 {
3     foreach(var item in items)
4     {
5         // do something with the item…
6     }
7 }

7.直接使用数字

 
1 if(mode == 1) { … }
2 else if(mode == 2) { … }
3 else if(mode == 3) { … }

为什么不给你的这些数字取个名字呢?比如使用Enumerations。

 
01 public enum SomeEnumerator
02 {
03     DefaultMode = 1,
04     SafeMode = 2,
05     NormalMode = 3
06 }
07   
08 if(mode == SomeEnumerator.DefaultMode) { … }
09 else if(mode == SomeEnumerator.SafeMode) { … }
10 else if(mode == SomeEnumerator.NormalMode) { … }

8.字符串替换

 
1 string s = "www.coolshell.cn is a amazing site";
2 s.Replace("amazing", "awful");

字符串s的内容什么也不会改变,因为string返回的是替换过的字串。这点很多初学者经常忘了。下面就没有问题了。

 
1 s = s.Replace("amazing", "awful");

来源:http://dev-the-web.com/blog/2009/08/27/top-csharp-programming-mistakes/

加支付宝好友偷能量挖...


评论(0)网络
阅读(135)喜欢(0)Asp.Net/C#/WCF