国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-18 17:09:58
字體:
來源:轉載
供稿:網友

擴展方法(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.VEVb.com/archive/2006/06/10/422676.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建昌县| 长葛市| 罗源县| 佛山市| 安龙县| 宁津县| 镇江市| 龙胜| 衡阳市| 鄂伦春自治旗| 尚义县| 大埔县| 迁安市| 长宁区| 白山市| 西乌珠穆沁旗| 从化市| 磴口县| 垣曲县| 三穗县| 瑞昌市| 永吉县| 玉林市| 仪陇县| 克什克腾旗| 叶城县| 河池市| 永城市| 宜川县| 南投县| 忻州市| 盐城市| 常宁市| 永定县| 安阳县| 郸城县| 西乡县| 西畴县| 天长市| 丰都县| 裕民县|