自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# 3.0新特性:擴(kuò)展方法

開(kāi)發(fā) 后端
本文介紹了c# 3.0新特性中如何使用擴(kuò)展方法,并且列出了定義擴(kuò)展方法的代碼,希望對(duì)大家有用。

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

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

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

3.第二個(gè)參數(shù)開(kāi)始對(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 { getprivate set; }  
  11.  
  12.         public string Name { getset; }  
  13.         public string City { getset; }  
  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#語(yǔ)言讀書(shū)心得備忘
  2. 詳解C#制做Active控件的五個(gè)步驟
  3. 總結(jié)C#多線程的點(diǎn)點(diǎn)滴滴
  4. 學(xué)習(xí)C#多線程:lock的用法
  5. 各種C#數(shù)組的定義和初始化
責(zé)任編輯:book05 來(lái)源: ajaxcn
相關(guān)推薦

2009-08-31 14:45:07

Visual C# 3

2009-09-01 11:19:47

C# 3.0擴(kuò)展重載抉

2009-09-18 15:53:37

C# 3.0新語(yǔ)言特性

2009-08-24 18:01:45

C#3.0新特性

2009-08-26 17:10:09

C# 3.5新特性

2009-08-10 17:36:17

C#擴(kuò)展方法

2009-08-27 18:04:01

c#擴(kuò)展方法string

2009-08-19 16:51:14

C# 4.0 dyna

2009-09-01 11:04:59

C#調(diào)用擴(kuò)展方法

2009-08-24 17:55:44

C#3.0新特性

2009-08-31 14:45:10

C#擴(kuò)展方法

2009-05-26 09:28:22

C# 4.0dynamic動(dòng)態(tài)類型

2009-08-18 14:14:45

C#擴(kuò)展方法性能測(cè)試

2009-08-27 09:27:49

C#擴(kuò)展方法

2009-08-26 15:53:48

C#擴(kuò)展方法

2016-10-13 13:33:41

反射特性c#

2009-08-13 09:46:49

C#歷史C# 4.0新特性

2009-07-27 09:46:28

Silverlight

2011-07-27 16:12:35

Linux KerneLinux內(nèi)核

2009-05-25 15:42:03

Visual StudC#
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)