C# 3.0给我们带来了很多新特性,其中增加了很多“动态”内容,
让我们使用起来更加Sharp!
我在这里简单的介绍一下C# 3.0规范中的一些“新鲜”内容,小弟才疏学浅,望大家海涵!
Part1:使用隐含类型的本地变量
在C#3.0之前的C#语言中,我们在声明变量的时候都必须显式的指定变量类型(int,string之类的)
我们一般都是这样写的:
1static void InitAndPrint()
2{
3 int x = 7;
4 string s = "This is a string.";
5 double d = 99.99;
6 int[] numbers = new int[] { 0, 1, 2, 3, 4, 5, 6 };
7
8 Console.WriteLine("x: " + x);
9 Console.WriteLine("s: " + s);
10 Console.WriteLine("d: " + d);
11 Console.WriteLine("numbers: ");
12 foreach (int n in numbers) Console.WriteLine(n);
13}
而在C# 3.0中,我们这样写:
1static void InitAndPrint()
2 {
3 var x = 7;
4 var s = "This is a string.";
5 var d = 99.99;
6 var numbers = new int[] { 0, 1, 2, 3, 4, 5, 6 };
7
8 Console.WriteLine("x: " + x);
| 对此文章发表了评论 |

