C# 3.0新特性初步研究 Part2:使用扩展方法

扩展方法(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 dds = 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}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12641349/viewspace-145394/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12641349/viewspace-145394/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值