這篇文章主要介紹了C# 3.0中擴(kuò)展方法怎么用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
太湖網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司成立于2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
Extension Methods 使用擴(kuò)展方法,使用的時(shí)候需要注意的地方
1.C# 3.0新特性中擴(kuò)展方法所屬的類(lèi)必須為靜態(tài)非泛型類(lèi),擴(kuò)展方法也是靜態(tài)方法
2.***個(gè)參數(shù)為被擴(kuò)展的類(lèi)型實(shí)例,并且必須用this進(jìn)行修飾
3.第二個(gè)參數(shù)開(kāi)始對(duì)對(duì)應(yīng)被擴(kuò)展類(lèi)型實(shí)例所傳遞的參數(shù)列表,即擴(kuò)展類(lèi)型實(shí)例
傳遞的***個(gè)參數(shù)對(duì)應(yīng)擴(kuò)展方法定義的第二個(gè)參數(shù),依次類(lèi)推
4.C# 3.0新特性中被擴(kuò)展類(lèi)型實(shí)例可像調(diào)用類(lèi)型內(nèi)部定義的實(shí)例方法一樣調(diào)用擴(kuò)展方法
這里定義一個(gè)擴(kuò)展方法:
public static class Extensions { public static bool Compare(this Customer customer1, Customer customer2) { if (customer1.CustomerId == customer2.CustomerId && customer1.Name == customer2.Name && customer1.City == customer2.City) { return true; } return false; } }
其中Compare***個(gè)參數(shù)用this修飾
完整源碼例子,這個(gè)例子主要查詢(xún)新建的newCustomer是否在列表List中
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NewLanguageFeatures { public class Customer { public int CustomerId { get; private set; } public string Name { get; set; } public string City { get; set; } public Customer(int Id) { CustomerId = Id; } public override string ToString() { return Name + “\t” + City + “\t” + CustomerId; } } public static class Extensions { public static bool Compare(this Customer customer1, Customer customer2) { if (customer1.CustomerId == customer2.CustomerId && customer1.Name == customer2.Name && customer1.City == customer2.City) { return true; } return false; } } class Program { static void Main(string[] args) { var customers = CreateCustomers(); var newCustomer = new Customer(10) { Name = “Stuart Glasson”, City = “Oxford” }; foreach (var c in customers) { if (newCustomer.Compare(c)) { Console.WriteLine(”The new customer was already in the list”); return; } } Console.WriteLine(”The new customer was not in the list”); } static List< Customer> CreateCustomers() { return new List< Customer> { new Customer(1) { Name = “Alex Roland”, City = “Berlin” }, new Customer(2) { Name = “Oliver Cox”, City = “Marseille” }, new Customer(3) { Name = “Maurice Taylor”, City = “London” }, new Customer(4) { Name = “Phil Gibbins”, City = “London” }, new Customer(5) { Name = “Tony Madigan”, City = “Torino” }, new Customer(6) { Name = “Elizabeth A. Andersen”, City = “Portland” }, new Customer(7) { Name = “Justin Thorp”, City = “London” }, new Customer(8) { Name = “Bryn Paul Dunton”, City = “Portland” } }; } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C# 3.0中擴(kuò)展方法怎么用”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!