国产探花免费观看_亚洲丰满少妇自慰呻吟_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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 三门峡市| 光山县| 永仁县| 海原县| 专栏| 息烽县| 安多县| 介休市| 唐山市| 商都县| 上高县| 台中县| 塔城市| 思茅市| 荆门市| 肥乡县| 淅川县| 佳木斯市| 荣昌县| 怀集县| 凤城市| 富宁县| 青浦区| 嘉兴市| 水城县| 特克斯县| 信丰县| 韶山市| 阜南县| 蕉岭县| 观塘区| 边坝县| 平邑县| 德惠市| 车致| 界首市| 临澧县| 札达县| 普定县| 江陵县| 铜鼓县|