All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Visual,Studio,2005集成开发环境图解(二)_[Asp.Net教程]
C#,3.0新特性系列:隐含类型var_.net资料_编程技术
技巧文集:PHP如何禁止图片文件的被盗链_php资料_编程技术
asp.net2.0用户控件的应用
开发环境下优化SQl语句的十个重要步骤_mssql学习_编程技术
C#类的声明方法_[Asp.Net教程]
VS2005,中调试JavaScript的方法_[Asp.Net教程]
在PHP中全面阻止SQL注入式攻击之三_[PHP教程]
用动态网页技术PHP打造个人网站全攻略_php资料_编程技术
在ASP.net中从SQL数据库保存取出图片(可用于上传图片)
Reading number is top 10 pictures
XuYing poker perspective garment debut
30 beautiful school beauty2
In the world the most mysterious 21 place landscape3
Nine school beauty star those gossip matters
Take you to walk into the most true north Korea rural2
Sell the barbecue as says father du breul2
湖南中医院的养生八图
美女浴室写真1
接财神,大吉大利,财源滚滚来
俄罗斯台球天后惊艳魅惑2
Download software ranking
金山office2007
虚拟机汉化软件
Call Of Duty2
linux安装大全
仙剑奇侠传98版歌曲
Unix video tutorial5
都市狐狸姑娘传
The Bermuda triangle3
尖东毒玫瑰B
C语言教程TXT
delv published in(发表于) 2014/1/6 9:05:39 Edit(编辑)
C#,3.0新特性初步研究,Part4:使用集合类型初始化器_[Asp.Net教程]

C#,3.0新特性初步研究,Part4:使用集合类型初始化器_[Asp.Net教程]

C# 3.0新特性初步研究 Part4:使用集合类型初始化器_[Asp.Net教程]

集合类型初始化器(Collection Initializers)


想看一段“奇怪”的代码:
1class Program
2 {
3 static void Main(string[] args)
4 {
5 var a = new Point { x = 10, y = 13 };
6 var b = new Point { x = 33, y = 66 };
7
8 var r1 = new Rectangle { p1 = a, p2 = b };
9 Console.WriteLine("r1: p1 = {0},{1}, p2 = {2},{3}",
10 r1.p1.x, r1.p1.y, r1.p2.x, r1.p2.y);
11
12 var c = new Point { x = 13, y = 17 };
13 var r2 = new Rectangle { p2 = c };
14
15 Console.WriteLine("r2: p1 == {0}, p2 = {1}, {2}",
16 r2.p1, r2.p2.x, r2.p2.y);
17 }
18 }
19
20 public class Point
21 {
22 public int x, y;
23 }
24 public class Rectangle
25 {
26 public Point p1, p2;
27 }
注意到集合类型的初始化语法了吗?直截了当!
这也是C# 3.0语法规范中的一个新特性。


也许下面的例子更能说明问题:
这是我们以前的写法:
1class Program
2 {
3 private static List keywords = new List();
4
5 public static void InitKeywords()
6 {
7 keywords.Add("while");
8 keywords.Add("for");
9 keywords.Add("break");
10 keywords.Add("switch");
11 keywords.Add("new");
12 keywords.Add("if");
13 keywords.Add("else");
14 }
15
16 public static bool IsKeyword(string s)
17 {
18 return keywords.Contains(s);
19 }
20 static void Main(string[] args)
21 {
22 InitKeywords();
23 string[] toTest = { "some", "identifiers", "for", "testing" };
24
25 foreach (string s in toTest)
26 if (IsKeyword(s)) Console.WriteLine("'{0}' is a keyword", s);
27 }
28 }
这是我们在C# 3.0中的写法:
1class Program
2 {
3 private static List keywords = new List {
4 "while", "for", "break", "switch", "new", "if", "else"
5 };
6
7 public static bool IsKeyword(string s)
8 {
9 return keywords.Contains(s);
10 }
11
12 static void Main(string[] args)
13 {
14 string[] toTest = { "some", "identifiers", "for", "testing" };
15
16 foreach (string s in toTest)
17 if (IsKeyword(s)) Console.WriteLine("'{0}' is a keyword", s);
18 }
19 }是不是变得像枚举类型的初始化了?
个人觉得这对提高代码的阅读质量是很有帮助的,
否则一堆Add()看上去不简洁,感觉很啰嗦。


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.