首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> C#教程 -> C# 3.0新特性初步研究 Part2:使用扩展方法
C# 3.0新特性初步研究 Part2:使用扩展方法


文章类别:C#教程 来源: 作者: 发表日期:2006-6-12 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

扩展方法(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}
怎%

http://zc1984.cnblogs.com/archive/2006/06/10/422676.html

上一篇:Photshop用风景照做特殊色调明信片 下一篇:C# 3.0新特性初步研究 Part3:使用拉姆达表达式
本栏目热门文章
·C#语言初级入门(1) 2005-10-4
·C# 中的类型转换 2006-4-10
·C#语言初级入门(3) 2005-10-4
·C#语言初级入门(2) 2005-10-4
·C#,深入浅出全接触(一) 2005-10-4
·c#学习笔记(1) 2005-10-4
·C# 3.0语言详解之基本的语言增强 2005-10-22
·C#的前途如何? 2005-10-4
·C#,深入浅出全接触(二) 2005-10-4
·基于C#的接口基础教程之一 2005-10-4
新近更新文章
·C# 3.0新特性初步研究 Part1:使用隐含类型的本地变量 2006-6-12
·C# 4.0语言将出现重大改变,带来一段Code Preview 2006-6-12
·C# 3.0新特性初步研究 Part6:使用查询表达式 2006-6-12
·C# 3.0新特性初步研究 Part5:匿名类型 2006-6-12
·C# 3.0新特性初步研究 Part4:使用集合类型初始化器 2006-6-12
·C# 3.0新特性初步研究 Part3:使用拉姆达表达式 2006-6-12
·C# 3.0新特性初步研究 Part2:使用扩展方法 2006-6-12
·c#泛型学习(二) 2006-5-12
·c#2.0泛型学习(一) 2006-5-12
·C# 编码规范和编程好习惯 2006-4-30
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: