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

Reading number is top 10 articles
黑客隐藏PHP文件后门的技巧_[PHP教程]
C#教程:线程的创建
C#中TabControl控件应用实例
浅谈水晶报表在ASP.NET中的一种灵活应用_[Asp.Net教程]
SQL语句过滤字符_[SQL,Server教程]
通过ADO.NET存取文件_[Asp.Net教程]
ASP.NET技巧:下载文件关闭窗体之解决方法_.net资料_编程技术
sql server 带列名导出至excel_[SQL Server教程]
Microsoft,SQLServer安装示例_mssql学习_编程技术
Community,Server专题一:概述Community,Server_[Asp.Net教程]
Reading number is top 10 pictures
So beauty, will let you spray blood7
再发两张抽象画
Kim jong il's mistress, national beauty JinYuJi actor2
Azusa Yamamoto2
中国的阶级现状
The money of more than 100 countries and regions21
Sora aoi in China4
In the world the most mysterious 21 place landscape1
Chinese paper-cut grilles art appreciation4
The money of more than 100 countries and regions13
Download software ranking
星际争霸1.08硬盘免安装版
Professional killers2 for Android
尖东毒玫瑰A
豪门浪荡史
双旗镇刀客B
Boxer's Top ten classic battle1
Twenty piece of palm leaf
Boxer's Top ten classic battle8
Eclipse 4.2.2 For Win64
Ashlynn Video3
delv published in(发表于) 2014/1/6 9:05:33 Edit(编辑)
C#,3.0新特性初步研究,Part2:使用扩展方法_[Asp.Net教程]

C#,3.0新特性初步研究,Part2:使用扩展方法_[Asp.Net教程]

C# 3.0新特性初步研究 Part2:使用扩展方法_[Asp.Net教程]

扩展方法(Extension Method)
可以为已有的类型添加新的方法定义和实现,比如int类型目前没有一个名叫xxxyyy()的方法,
那么通过使用扩展方法,我们可以为int类型添加一个xxxyyy()方法。
这个有点类似于用来扩展系统功能的某些设计模式。


下面我们用代码来说话:
这是我们以前的写法:


1public static class Extensions
2{
3 public static string CamelCase(string identifier)
4{
5 string newString = "";
6 bool sawUnderscore = false;
7
8 foreach (char c in identifier)
9 {
10 if ((newString.Length == 0) && Char.IsLetter(c))
11 newString += Char.ToUpper(c);
12 else if (c == '_')
13 sawUnderscore = true;
14 else if (sawUnderscore)
15 {
16 newString += Char.ToUpper(c);
17 sawUnderscore = false;
18 }
19 else
20 newString += c;
21 }
22
23 return newString;
24}
25}
26
27static void Main(string[] args)
28{
29string[] identifiers = new string[] {
30 "do_something",
31 "find_all_objects",
32 "get_last_dict_entry"
33 };
34
35foreach (string s in identifiers)
36 Console.WriteLine("{0} becomes: {1}", s, Extensions.CamelCase(s));
37}
38
C# 3.0中我们可以这样写:
1public static class Extensions
2{
3 public static string CamelCase(this string identifier)
4{
5 string newString = "";
6 bool sawUnderscore = false;
7
8 foreach (char c in identifier)
9 {
10 if ((newString.Length == 0) && Char.IsLetter(c))
11 newString += Char.ToUpper(c);
12 else if (c == '_')
13 sawUnderscore = true;
14 else if (sawUnderscore)
15 {
16 newString += Char.ToUpper(c);
17 sawUnderscore = false;
18 }
19 else
20 newString += c;
21 }
22
23 return newString;
24}
25}
26
27static void Main(string[] args)
28{
29string[] identifiers = new string[] {
30 "do_something",
31 "find_all_objects",
32 "get_last_dict_entry"
33 };
34
35foreach (string s in identifiers)
36 Console.WriteLine("{0} becomes: {1}", s, Extensions.CamelCase(s));
37}
主要是下面这两个语句的变化:
1public static string CamelCase(this string identifier)
2Console.WriteLine("{0} becomes: {1}", s, s.CamelCase());
变量s原本是一个string类型,并没有CamelCase()方法,但是我们在CamelCase()方法的参数列表最前面加上一个this关键字,
则string s就拥有了一个新的方法CamelCase,很简单也很直接 :)


下面我们看一看一个稍微复杂一点的应用:
1public static class Extensions
2{
3public static List Combine(this List a, List b)
4{
5 var newList = new List(a);
6 newList.AddRange(b);
7 return newList;
8}
9}
10
11static void Main(string[] args)
12{
13var odds = new List();
14odds.Add(1);
15odds.Add(3);
16odds.Add(5);
17odds.Add(7);
18
19var evens = new List();
20evens.Add(0);
21evens.Add(2);
22evens.Add(4);
23evens.Add(6);
24
25var both = odds.Combine(evens);
26Console.WriteLine("Contents of 'both' list:");
27foreach (int i in both)
28 Console.WriteLine(i);
29}


来源:网络







添加到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.