C#编程入门的八大禁忌

开发 后端
这里是C#编程入门的八点注意事项,包括对字符串String变量的使用,字符器的连接、字符串转整型等等内容,希望对大家有所帮助。

作为微软.NET平台的“一等公民”,C#是大家必须学习的。那么在C#编程入门时,我们必定要走很多弯路,这里介绍的一些注意事项,是为了避免犯不必要的错误。

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

1.使用String变量:

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

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

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

  1. view sourceprint?
  2. if  (!String.IsNullOrEmpty(someString) && someString.Length > 0) 
  3. {       
  4. // 是不是更好一些? 
  5. } 

2.字符器连接

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

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

  1. view sourceprint?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. view sourceprint?  
  2. Console.WriteLine("A= " + 1 + " B=" + 2 + " C= " + someValue); 

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

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

4.字符串转整型

  1. view sourceprint?  
  2. int i = int.Parse(Request.QueryString["id"]); 

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

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

5. 调用IDbConnection 的 Close 方法

  1. view sourceprint?  
  2. IDbConnection dbConn = null;   
  3.  try{       
  4. dbConn = new SqlConnection(”some Connection String”);       
  5. dbConn.Open();   
  6. }   
  7. finally{     
  8. dbConn.Close(); } 

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

  1. view sourceprint?  
  2. IDbConnection dbConn = null;   
  3.  try{       
  4. dbConn = new SqlConnection(”Some Connection String”);   
  5. dbConn.Open(); }   
  6. finally09.{      
  7. if (dbConn != null)       
  8. {           
  9. dbConn.Close();      
  10. }  

6.使用List类

  1. view sourceprint?  
  2. public void SomeMethod(List items) {       
  3. foreach(var item in items)       
  4. {           
  5. // do something with the item…       
  6. } } 

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

  1. view sourceprint?  
  2. public void SomeMethod(IEnumerable items)   
  3. {       
  4. foreach(var item in items)       
  5. {           
  6. // do something with the item…      
  7. } } 

7.直接使用数字

  1. view sourceprint?  
  2. if(mode == 1) { … }   
  3. else if(mode == 2) { … }   
  4. else if(mode == 3) { … } 

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

  1. view sourceprint?  
  2. public enum SomeEnumerator {      
  3. DefaultMode = 1,      
  4. SafeMode = 2,      
  5. NormalMode = 3 }   
  6. if(mode == SomeEnumerator.DefaultMode) { … }   
  7. else if(mode == SomeEnumerator.SafeMode) { … }   
  8. else if(mode == SomeEnumerator.NormalMode) { … } 

8.字符串替换

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

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

  1. view sourceprint?  
  2. s = s.Replace("amazing""awful"); 

C#编程入门的八点注意事项就介绍到这里。

【编辑推荐】

  1. C#创建快捷方式简单描述
  2. C#压缩Access数据库详细介绍
  3. C#实现加载动态库概述
  4. C#日期型数据简单剖析
  5. C#装箱和拆箱简单描述
责任编辑:彭凡 来源: coolshell.cn
相关推荐

2011-04-14 18:03:49

2010-08-05 16:45:20

白领禁忌

2011-04-28 15:11:53

打印办公设备

2022-04-25 13:11:14

Python编程技巧

2024-02-27 07:12:12

编程语言TS

2009-06-22 14:07:46

JSF优势

2023-11-15 15:45:22

2023-10-17 15:29:44

2009-08-26 10:34:15

C#类型C#变量

2011-04-29 09:15:16

Servlet

2024-04-24 09:52:19

云技能云迁移云计算

2023-12-27 11:45:09

2012-05-10 16:45:54

linux系统

2022-01-05 09:26:56

IT灾难IT故障

2011-08-17 13:55:25

VoIPPBX

2012-05-05 09:28:50

三星

2012-05-11 11:53:36

虚拟化

2011-07-11 14:01:12

JAVA

2020-06-28 14:01:50

漏洞管理漏洞攻击

2022-05-07 11:13:58

苹果汽车技术
点赞
收藏

51CTO技术栈公众号