教程中国
PHOTOSHOP CS9.0中文版 MAYA 8.5 FOR WINDOWS Corel Painter v9.0 Flash MX2004 中文版 Illustrator cs2 中文版
VC++6.0含sp6 中英文版 VB6.0 +sp6 简体中文版 Borland Delphi 7汉化版 MSDN for vb6.0中文版 Visual Studio 2005简体
教程中国下属 文件存储共享专家BIBIDU.COM 提供大型软件,教材,源码,电影,音乐,图书等下载 更多精品请点此进入
  您目前所在位置: 教程中国 >> 编程基地 >> C# >> C# 3.0新特性初步研究 Part2:使用扩展方法 RSS订阅
C# 3.0新特性初步研究 Part2:使用扩展方法
教程(视频,书籍)下载:  ASP.NET AutoCAD 数据库 C# ASP java photoshop 网页设计 delphi 3dmax Flash C++ VB 张孝祥 实例   更多请进入BIBIDU搜索
IT搜索引擎   
扩展方法(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<T> Combine<T>(this List<T> a, List<T> b)
 4{
 5    var newList = new List<T>(a);
 6    newList.AddRange(b);
 7    return newList;
 8}
    
 9}

10
11static void Main(string[] args)
12{
13var odds = new List<int>();
14odds.Add(1);
15odds.Add(3);
16odds.Add(5);
17odds.Add(7);
18
19var evens = new List<int>();
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}

怎%


来源:upschool.com.cn
作者:
关键字:Part2,使用扩展方法
发表日期:2006-6-10

网页显示有限 阅读全文请下载本文完整版WORD文档

上一篇:C# 3.0新特性初步研究 Part1:使用隐含类型的本地变量   下一篇:C# 3.0新特性初步研究 Part3:使用拉姆达表达式


2008-11-21 18:21:44
本文的相类似文章
  • 说一说如何配置Oracle的MTS part2
  • C# 3.0新特性初步研究 Part2:使用扩展方法
  • DataGrid Web控件深度历险(3) part2
  • DataGrid Web控件深度历险(2) Part2
  • DPC:Creating a DataBound List of Radio Buttons--PART2[等级:中]
  • part2: ShowIssueCat.aspx
  • 防止全局hook入侵Delphi版,2000以上系统适用(part2)
  • 在学习中进步 在进步中成长 教程中国相随您的成长之路
    华腾联合科技股份有限公司版权所有
    广告联系:Rosibo@163.com