CSharp3.0锐利初体验(一)
装了VS2008 beta 2 ,今天小爽了一把CSharp3.0,总的感受就是微软帮程序员又分担了很多负担,编译器的编写者试着将程序员的一些冗余工作交给编译器去完成,程序员写起代码来更加顺畅。从语言的角度来讲,C#的抽象程度越来越高了。
下面这个程序包含了一部分C#3.0的新特征:
1
using System;
2
3
using System.Collections.Generic;
4
5
using System.Text;
6
7
using System.Threading;
8
9
10
11
namespace CSharp3
12
13
{
14
15
class Program
16
17
{
18
19
static void Main(string[] args)
20
21
{
22
23
//扩展方法
24
25
string s = "aa2ss";
26
27
s.ExMethod();
28
29
30
31
//对象初始化器
32
33
//对象初始化器的出现,减少了冗余代码
34
35
//直接在实例化类的同时就初始化了字段,但是这跟构造函数是不同的
36
37
myPoint my = new myPoint { X = 0, Y = 1 };
38
39
Console.WriteLine("X of my =" + my.X.ToString());
40
41
Console.ReadKey();
42
43
44
45
//匿名类型
46
47
//编译器会根据属性来推断相应的类型
48
49
//有var的地方就有推断
50
51
Console.Clear();
52
53
var p1 = new { X = 32, Y = 45 };
54
55
Console.WriteLine(
56
57
"p1 is :{0},and p1.X={1}" ,
58
59
p1.GetType().ToString(),p1.X);
60
61
Console.ReadKey();
62
63
64
65
//Lambda表达式
66
67
Console.Clear();
68
69
LambdaClass.testLambda1();
70
71
Console.WriteLine("********************");
72
73
LambdaClass.testLambda2();
74
75
Console.ReadKey();
76
77
}
78
79
80
81
}
82
83
84
85
/// <summary>
86
87
/// 扩展方法
88
89
/// </summary>
90
91
public static class Extensions
92
93
{
94
95
/// <summary>
96
97
/// 扩展方法实际上提供了一种给已定义类添加或者追加方法的方便途径
98
99
/// 这里this关键字是个标示符,this后面的类型定义了要给哪个类追加方法
100
101
/// </summary>
102
103
/// <param name="s"></param>
104
105
public static void ExMethod(this string s)
106
107
{
108
109
Console.WriteLine("This is a Extension Method!!");
110
111
Console.ReadKey();
112
113
Console.Clear();
114
115
}
116
117
}
118
119
120
121
/// <summary>
122
123
/// 对象初始化器例子
124
125
/// </summary>
126
127
public class myPoint
128
129
{
130
131
int x, y;
132
133
134
135
public int Y
136
137
{
138
139
get { return y; }
140
141
set { y = value; }
142
143
}
144
145
public int X
146
147
{
148
149
get { return x; }
150
151
set { x = value; }
152
153
}
154
155
}
156
157
158
159
# region Lambda Expressions
160
161
162
163
public delegate int myDelegate(int a,int b);
164
165
166
167
/// <summary>
168
169
/// Lambda表达式本身是一个委托类型
170
171
/// </summary>
172
173
public class LambdaClass
174
175
{
176
177
public static void myMethod(myDelegate myDele)
178
179
{
180
181
Console.WriteLine(
182
183
"Let's have a see what was represented by the delegate:\n{0}",
184
185
myDele.Method.ToString());
186
187
Console.WriteLine("the result of culculating between 8 & 8 is :");
188
189
Console.WriteLine(myDele.Invoke(8, 8));
190
191
}
192
193
194
195
/// <summary>
196
197
/// 甚至无须声明参数类型,因为编译器会在匹配的委托里找到它们的类型
198
199
/// </summary>
200
201
public static void testLambda1()
202
203
{
204
205
LambdaClass.myMethod((x,y) =>x*y);
206
207
}
208
209
210
211
/// <summary>
212
213
/// 很自然的方式,可以替代匿名方法,或者作为一个匿名的委托
214
215
/// </summary>
216
217
public static void testLambda2()
218
219
{
220
221
Thread t = new Thread(() =>
222
223
{
224
225
Console.WriteLine("This is a Lambda Expression!");
226
227
});
228
229
t.Start();
230
231
}
232
233
}
234
235
#endregion
236
237
}
238
239
using System;2

3
using System.Collections.Generic;4

5
using System.Text;6

7
using System.Threading;8

9
10

11
namespace CSharp312

13
{14

15
class Program16

17
{18

19
static void Main(string[] args)20

21
{22

23
//扩展方法24

25
string s = "aa2ss";26

27
s.ExMethod();28

29
30

31
//对象初始化器32

33
//对象初始化器的出现,减少了冗余代码34

35
//直接在实例化类的同时就初始化了字段,但是这跟构造函数是不同的36

37
myPoint my = new myPoint { X = 0, Y = 1 };38

39
Console.WriteLine("X of my =" + my.X.ToString());40

41
Console.ReadKey();42

43
44

45
//匿名类型46

47
//编译器会根据属性来推断相应的类型48

49
//有var的地方就有推断50

51
Console.Clear();52

53
var p1 = new { X = 32, Y = 45 };54

55
Console.WriteLine(56

57
"p1 is :{0},and p1.X={1}" ,58

59
p1.GetType().ToString(),p1.X);60

61
Console.ReadKey();62

63
64

65
//Lambda表达式 66

67
Console.Clear();68

69
LambdaClass.testLambda1();70

71
Console.WriteLine("********************");72

73
LambdaClass.testLambda2();74

75
Console.ReadKey();76

77
}78

79
80

81
}82

83
84

85
/// <summary>86

87
/// 扩展方法88

89
/// </summary>90

91
public static class Extensions92

93
{94

95
/// <summary>96

97
/// 扩展方法实际上提供了一种给已定义类添加或者追加方法的方便途径98

99
/// 这里this关键字是个标示符,this后面的类型定义了要给哪个类追加方法100

101
/// </summary>102

103
/// <param name="s"></param>104

105
public static void ExMethod(this string s)106

107
{108

109
Console.WriteLine("This is a Extension Method!!");110

111
Console.ReadKey();112

113
Console.Clear();114

115
}116

117
}118

119
120

121
/// <summary>122

123
/// 对象初始化器例子124

125
/// </summary>126

127
public class myPoint128

129
{130

131
int x, y;132

133
134

135
public int Y136

137
{138

139
get { return y; }140

141
set { y = value; }142

143
}144

145
public int X146

147
{148

149
get { return x; }150

151
set { x = value; }152

153
}154

155
}156

157
158

159
# region Lambda Expressions160

161
162

163
public delegate int myDelegate(int a,int b);164

165
166

167
/// <summary>168

169
/// Lambda表达式本身是一个委托类型170

171
/// </summary>172

173
public class LambdaClass174

175
{176

177
public static void myMethod(myDelegate myDele)178

179
{180

181
Console.WriteLine(182

183
"Let's have a see what was represented by the delegate:\n{0}",184

185
myDele.Method.ToString());186

187
Console.WriteLine("the result of culculating between 8 & 8 is :");188

189
Console.WriteLine(myDele.Invoke(8, 8));190

191
}192

193
194

195
/// <summary>196

197
/// 甚至无须声明参数类型,因为编译器会在匹配的委托里找到它们的类型198

199
/// </summary>200

201
public static void testLambda1()202

203
{204

205
LambdaClass.myMethod((x,y) =>x*y);206

207
}208

209
210

211
/// <summary>212

213
/// 很自然的方式,可以替代匿名方法,或者作为一个匿名的委托214

215
/// </summary>216

217
public static void testLambda2()218

219
{220

221
Thread t = new Thread(() =>222

223
{224

225
Console.WriteLine("This is a Lambda Expression!");226

227
});228

229
t.Start(); 230

231
}232

233
}234

235
#endregion236

237
}238

239

的确写得比较爽,体现了“以人文本”的思想. 编译器似乎是程序员的一个朋友,为程序员分担了很多工作,编译器会经常猜到程序员写的是什么,当然前提是你写的程序为编译器的推断提供了充分合理的理由。我想这也是一个趋势吧,让程序员coding时更惬意,更自然,更像”人”一样开发,而不是处处要迎合机器的思维.恩,也许不久以后CSharp会像word一样普及的,期待中...


浙公网安备 33010602011771号