初学C#编程的注意事项

Code:
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Data.SqlClient;
  6. usingSystem.Data;
  7. usingSystem.Collections;
  8. namespace面试题目
  9. {
  10. class初学者注意
  11. {
  12. publicvoidnotice()
  13. {
  14. //1.使用string变量
  15. stringtest="dinglang";
  16. //判断字符串是否有内容
  17. if(test.Length>0)
  18. {
  19. }
  20. //但是,这个对象很可能为空,所以判断是否为null
  21. if(!string.IsNullOrEmpty(test))
  22. {
  23. }
  24. //2.字符串拼接
  25. //这样做没错。+=实际是调用了String类的Append访问,而且会重新生成新的s对象,影响性能、效率
  26. strings="a";
  27. s+="b";
  28. s+="c";
  29. s+="d";
  30. //提倡用下面这种方式拼接
  31. StringBuildersb=newStringBuilder();
  32. sb.Append("a");
  33. sb.Append("b");
  34. sb.Append("c");
  35. sb.Append("d");
  36. //3.使用Console
  37. Console.WriteLine("字符串test="+test+"字符串s="+s);//效率更低
  38. Console.WriteLine("字符串test:{0}/ns:{1}",test,s);//使用占位符{},换行符/n后效率更高
  39. //4.字符串转换成整型
  40. inti=int.Parse(test);//很可能会抛出异常
  41. i=Convert.ToInt32(test);//如果test为null,会返回0使用(int)i方式会强制转换
  42. if(int.TryParse(test,outi))
  43. {
  44. //使用TryParse的方式会好点
  45. }
  46. //5.调用IDbConnection的Close方法
  47. IDbConnectionconn=null;
  48. try{
  49. conn=newSqlConnection("");
  50. conn.Open();
  51. }
  52. finally{
  53. conn.Close();
  54. }
  55. //调用SqlConnection的构造函数可能会出现一个异常,如果是这样的话,我们还需要调用Close方法吗?
  56. try{
  57. conn=newSqlConnection("");
  58. conn.Open();
  59. }
  60. finally{
  61. if(conn!=null)
  62. {
  63. conn.Close();
  64. }
  65. }
  66. //6.遍历List
  67. //publicvoiddoSome(List<int>list)
  68. //{
  69. //foreach(variteminlist)
  70. //{
  71. ////item
  72. //}
  73. //}
  74. //如果只遍历List容器中的所有内容的话,那么,使用IEnumerable接口会更好一些。因为函数参数传递一个List对象要比一个IEnumerable接口要花费更多的开销。
  75. //publicvoiddoSome(IEnumerable<int>list)
  76. //{
  77. //foreach(variteminlist)
  78. //{
  79. ////item
  80. //}
  81. //}
  82. //7.直接使用数字
  83. if(i==1)
  84. {
  85. }
  86. elseif(i==2)
  87. {
  88. }
  89. elseif(i==3)
  90. {
  91. }
  92. //为什么不使用枚举呢?注意,要定义在函数外
  93. //publicenumSomenums
  94. //{
  95. //firstNum=1,
  96. //secondNum=2,
  97. //thirdNum=3
  98. //}
  99. //if(i=Somenums.firstNum)
  100. //{
  101. //}
  102. //elseif(Somenums.secondNum)
  103. //{
  104. //}
  105. //elseif(Somenums.thirdNum)
  106. //{
  107. //}
  108. //8.字符串替换,截取
  109. stringname="dinglang";
  110. name.Replace("d","D");
  111. Console.WriteLine(name);//奇怪,明明替换了,怎么打印出来还是“dinglang”啊?
  112. name.Substring(0,4);
  113. Console.WriteLine(name);//奇怪呀!明明截取了,怎么打印出来却还是“dinglang”啊?
  114. //哈哈。这是初学者,甚至...经常犯的错。
  115. name=name.Replace("d","D");
  116. Console.WriteLine(name);
  117. name.Substring(0,4);
  118. Console.WriteLine(name);
  119. //明白了吧。Replace、Substring等函数,其实是返回一个值,而并不会改变变量name的值。得使用name接收返回值,name的值才会改变。
  120. }
  121. }
  122. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值