av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

C# 3.0新特性:擴展方法

Extension Methods 使用擴展方法,使用的時候需要注意的地方

創(chuàng)新互聯(lián)基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺為眾多戶提供成都服務器托管 四川大帶寬租用 成都機柜租用 成都服務器租用。

1.C# 3.0新特性中擴展方法所屬的類必須為靜態(tài)非泛型類,擴展方法也是靜態(tài)方法

2.***個參數(shù)為被擴展的類型實例,并且必須用this進行修飾

3.第二個參數(shù)開始對對應被擴展類型實例所傳遞的參數(shù)列表,即擴展類型實例

傳遞的***個參數(shù)對應擴展方法定義的第二個參數(shù),依次類推

4.C# 3.0新特性中被擴展類型實例可像調(diào)用類型內(nèi)部定義的實例方法一樣調(diào)用擴展方法

這里定義一個擴展方法:

 
 
 
  1. public static class Extensions  
  2.     {  
  3.         public static bool Compare(this Customer customer1, Customer customer2)  
  4.         {  
  5.             if (customer1.CustomerId == customer2.CustomerId &&  
  6.                 customer1.Name == customer2.Name &&  
  7.                 customer1.City == customer2.City)  
  8.             {  
  9.                 return true;  
  10.             }  
  11.  
  12.             return false;  
  13.         }  
  14.  
  15.     } 

其中Compare***個參數(shù)用this修飾

完整源碼例子,這個例子主要查詢新建的newCustomer是否在列表List中

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace NewLanguageFeatures  
  7. {  
  8.     public class Customer  
  9.     {  
  10.         public int CustomerId { get; private set; }  
  11.  
  12.         public string Name { get; set; }  
  13.         public string City { get; set; }  
  14.  
  15.         public Customer(int Id)  
  16.         {  
  17.             CustomerId = Id;  
  18.         }  
  19.  
  20.         public override string ToString()  
  21.         {  
  22.             return Name + “\t” + City + “\t” + CustomerId;  
  23.         }  
  24.     }  
  25.     public static class Extensions  
  26.     {  
  27.         public static bool Compare(this Customer customer1, Customer customer2)  
  28.         {  
  29.             if (customer1.CustomerId == customer2.CustomerId &&  
  30.                 customer1.Name == customer2.Name &&  
  31.                 customer1.City == customer2.City)  
  32.             {  
  33.                 return true;  
  34.             }  
  35.  
  36.             return false;  
  37.         }  
  38.  
  39.     }  
  40.  
  41.     class Program  
  42.     {  
  43.         static void Main(string[] args)  
  44.         {  
  45.             var customers = CreateCustomers();  
  46.  
  47.             var newCustomer = new Customer(10)  
  48.             {  
  49.                 Name = “Stuart Glasson”,  
  50.                 City = “Oxford”  
  51.             };  
  52.  
  53.             foreach (var c in customers)  
  54.             {  
  55.       
  56.                 if (newCustomer.Compare(c))  
  57.                 {  
  58.                     Console.WriteLine(”The new customer was already in the list”);  
  59.                     return;  
  60.                 }  
  61.  
  62.             }  
  63.  
  64.             Console.WriteLine(”The new customer was not in the list”);  
  65.  
  66.         }  
  67.  
  68.         static List< Customer> CreateCustomers()  
  69.         {  
  70.             return new List< Customer>  
  71.                 {  
  72.                     new Customer(1) { Name = “Alex Roland”,             City = “Berlin”    },  
  73.                     new Customer(2) { Name = “Oliver Cox”,              City = “Marseille” },  
  74.                     new Customer(3) { Name = “Maurice Taylor”,          City = “London”    },  
  75.                     new Customer(4) { Name = “Phil Gibbins”,            City = “London”    },  
  76.                     new Customer(5) { Name = “Tony Madigan”,            City = “Torino”    },  
  77.                     new Customer(6) { Name = “Elizabeth A. Andersen”,   City = “Portland”  },  
  78.                     new Customer(7) { Name = “Justin Thorp”,            City = “London”    },  
  79.                     new Customer(8) { Name = “Bryn Paul Dunton”,        City = “Portland”  }  
  80.                 };  
  81.         }  
  82.     } 

C# 3.0新特性中的擴展方法就介紹到這里,希望對大家有用。

【編輯推薦】

  1. C#語言讀書心得備忘
  2. 詳解C#制做Active控件的五個步驟
  3. 總結(jié)C#多線程的點點滴滴
  4. 學習C#多線程:lock的用法
  5. 各種C#數(shù)組的定義和初始化

網(wǎng)站標題:C# 3.0新特性:擴展方法
分享鏈接:http://uogjgqi.cn/article/dhhshdh.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流