In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use the extension method in C# 3.0, it has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Extension Methods uses extension methods and needs to pay attention to when using them.
In the new features of 1.C# 3.0, the class to which the extension method belongs must be a static non-generic class, and the extension method is also a static method.
2. Parameters * are instances of the type being extended and must be decorated with this
3. The second parameter begins to list the parameters passed by the instance of the extended type, that is, the instance of the extended type.
The passed * * parameters correspond to the second parameter defined by the extension method, and so on
An instance of an extended type in the new feature of 4.C# 3.0 can call an extension method in the same way as an instance method defined within the type
An extension method is defined here:
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*** parameters are modified with this
Complete source code example, this example mainly queries whether the newly created newCustomer is in the list 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 = newCustomer (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"}} }} Thank you for reading this article carefully. I hope the article "how to use the extension method in C# 3.0" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.