01. C # data type, sorting, and filtering (chapter 1, 1.1-1.2 ),

Source: Internet
Author: User

01. C # data type, sorting, and filtering (chapter 1, 1.1-1.2 ),

As I read more articles and discussions, the more I find myself too uncertain. I will take a look at "C # Deep understanding" and take some Reading Notes to find my own shortcomings. If you don't talk about it, go to the topic.

Set a simple data type in C #1

 1 public class Product1 2     { 3         private string name; 4         private long price; 5  6         public string Name { get { return name; } } 7         public long Price { get { return price; } } 8  9         public Product1(string n, long p)10         {11             this.name = n;12             this.price = p;13         }14 15         public static ArrayList GetProducts()16         {17             ArrayList list = new ArrayList();18             list.Add(new Product1("cat", 1));19             list.Add(new Product1("fish", 2));20             list.Add(new Product1("dog", 3));21             list.Add(new Product1("pig", 4));22             return list;23         }24     }

C #1 does not have the concept of generics. The static method in the Product1 class returns the ArrayList type, and the element type in it is of course product1. After the generic type is introduced in C #2, the class can be defined as (renamed as Product2)

 1 public class Product2 2     { 3         private string name; 4         private long price; 5  6         public string Name { get { return name; } private set { name = value; } } 7         public long Price { get { return price; } private set { price = value; } } 8  9         public Product2(string n, long p)10         {11             Name = n;12             Price = p;13         }14 15         public static List<Product2> GetProducts()16         {17             List<Product2> list = new List<Product2>();18             list.Add(new Product2("cat", 1));19             list.Add(new Product2("fish", 2));20             list.Add(new Product2("dog", 3));21             list.Add(new Product2("pig", 4));22             return list;23         }24     }

Compared with C #3, attributes are improved by introducing the concept of automatic attribute implementation. The private attribute name and price in Product1 and Product2 can be written through automatic attributes, as shown below:

 1 class Product3 2     { 3         public string Name { get; private set; } 4         public long Price { get; private set; } 5  6         public Product3(string n, long p) 7         { 8             Name = n; 9             Price = p;10         }11 12         public static List<Product3> GetProducts()13         {14             List<Product3> list = new List<Product3> {15                 new Product3("cat",1),16                 new Product3("fish",2),17                 new Product3("dog",3),18                 new Product3("pig",4)19             };20             return list;21         }22     }

The class implementation in C #4 is mainly reflected in the class instantiation by introducing the named real parameters. Pay attention to the Class Object Instantiation in the GetProducts method below.

 1 class Product4 2     { 3         readonly string name; 4         readonly long price; 5         public string Name { get { return name; } } 6         public long Price { get { return price; } } 7  8         public Product4(string n, long p) 9         {10             name = n;11             price = p;12         }13 14         public static List<Product4> GetProducts()15         {16             return new List<Product4>17             {18                 new Product4(n:"cat",p:1),19                 new Product4(n:"fish",p:2),20                 new Product4(n:"dog",p:3),21                 new Product4(n:"pig",p:4)22             };23         }24     }

For example, new Product4 (n: "cat", p: 1), in the format of [parameter: parameter value], the value of a specified parameter can be displayed in the instantiation.

Next, let's talk about the implementation of sorting methods in C # Evolution

In C #1, you need to define a class that implements the IComparer interface.

1 // use IComparer to sort ArrayList 2 public class ComparerName1: IComparer 3 {4 public int Compare (object x, object y) 5 {6 Product1 p1 = (Product1) x; 7 Product1 p2 = (Product1) y; 8 return p1.Name. compareTo (p2.Name); 9} 10}

Instantiate the above class on the function page

 1 class Program 2     { 3         static void Main(string[] args) 4         { 5             ArrayList list = Product1.GetProducts(); 6  7             list.Sort(new ComparerName1()); 8  9             foreach (Product1 p in list)10             {11                 Console.WriteLine(p.Name);12             }13             Console.ReadKey();14 15         }16     }

It can be seen that the ArrayList type has a public interface (used for sorting Sort). The input parameter is an instance that implements the IComparer interface, exactly the ComparerName1 defined above (sorted by product name), how is it implemented in C #2?

As mentioned above, C #2 introduces the concept of generics (corresponding product class is Product2 class) and defines a class that implements the IComparer <Product2> interface.

1 public class ComparerName2 : IComparer<Product2>2     {3         public int Compare(Product2 x, Product2 y)4         {5             return x.Name.CompareTo(y.Name);6         }7     }

The usage of the function page is the same as that of C #1. The main difference is that ComparerName1 needs to forcibly convert the Object type to the Product1 type, because the specific type is known, the performance loss caused by forced conversion is avoided.
.

The automatic attributes in C #3 have no effect on sorting, but you can use the introduced Lambda expression to further streamline the sorting code.

1 class Program 2 {3 static void Main (string [] args) 4 {5 // sort by Lambda expression in C #3 6 List <Product3> list = Product3.GetProducts (); 7 8 list. sort (9 (x, y) => x. name. compareTo (y. name) 10); 11 12 foreach (Product3 p in list) 13 {14 Console. writeLine (p. name); 15} 16 Console. readKey (); 17} 18}

The implementation of Lambda expressions is actually delegate, and the same is true for delegate implementation.

The following describes how to query and print data.

C #1

1 class Program 2 {3 static void Main (string [] args) 4 {5 ArrayList list = Product1.GetProducts (); 6/* 7 C #1 use query, test, print 8 */9 foreach (Product1 p in list) 10 {11 if (p. price> 2) 12 {13 Console. writeLine (p. name); 14} 15} 16 Console. readKey (); 17 18} 19}

The query implementation in C #2 can be delegated.

1 class Program 2 {3 static void Main (string [] args) 4 {5 List <Product2> list = Product2.GetProducts (); 6 7 // C #2 use the anonymous method to create the delegate 8 9 Predicate <Product2> test = delegate (Product2 p) {return p. price> 2 ;}; 10 List <Product2> matches = list. findAll (test); 11 Action <Product2> print = delegate (Product2 p) {Console. writeLine (p. name) ;}; 12 matches. forEach (print); 13 list. findAll (test ). forEach (print); 14 15 Console. readKey (); 16 17} 18}

The result is the same as that in C #1. The print price is greater than the name of product 2, and it is more streamlined when it reaches C #3 because of the Lambda expression.

1 class Program 2 {3 static void Main (string [] args) 4 {5 List <Product3> list = Product3.GetProducts (); 6 7 // C #3 Use Lambda expressions to query 8 lists. findAll (x => x. price> 2 ). forEach (x => Console. writeLine (x. name); 9 10 Console. readKey (); 11 12} 13}

Here, we have reason to believe that Lambda expressions are disguised delegates, so we can introduce an idea that Lambda expressions can be used to replace them. Great !!!

Please make an axe.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.