日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C# 3.0新特性:擴(kuò)展方法

Extension Methods 使用擴(kuò)展方法,使用的時(shí)候需要注意的地方

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

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

2.***個(gè)參數(shù)為被擴(kuò)展的類型實(shí)例,并且必須用this進(jìn)行修飾

3.第二個(gè)參數(shù)開始對(duì)對(duì)應(yīng)被擴(kuò)展類型實(shí)例所傳遞的參數(shù)列表,即擴(kuò)展類型實(shí)例

傳遞的***個(gè)參數(shù)對(duì)應(yīng)擴(kuò)展方法定義的第二個(gè)參數(shù),依次類推

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

這里定義一個(gè)擴(kuò)展方法:

 
 
 
  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***個(gè)參數(shù)用this修飾

完整源碼例子,這個(gè)例子主要查詢新建的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新特性中的擴(kuò)展方法就介紹到這里,希望對(duì)大家有用。

【編輯推薦】

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

當(dāng)前名稱:C# 3.0新特性:擴(kuò)展方法
分享地址:http://www.5511xx.com/article/dhhshdh.html