• 01.C#数据类型、排序、过滤(一章1.1-1.2)


      随着看的文章及探讨越多,越发现自己实在太不定性了,看下《C#深入理解》吧,做一下读书笔记,从中发现自己的不足。闲话不说,进入正题吧。

      在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中没有泛型的概念,在Product1类中的静态方法返回是的ArrayList类型,里面的元素类型当然是Product1。而在C#2中引入泛型后,该类可定义为(重命名为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     }

      相比较于C#3,对于属性的改进,则是引入自动实现属性的概念,Product1和Product2中私有属性name和price,可以通过自动属性进行书写,如下

     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     }

      C#4中对类的实现主要体现在类的实例化中,引入命名实参,注意下面GetProducts方法中类对象的实例化

     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     }

      如new Product4(n:"cat",p:1),格式如[参数:参数值],在实例化中可以显示的指定参数的值。

      接下来说下C#进化过程中对排序方法的实现

      在C#1中,需要定义一个实现于IComparer接口的类

     1 //使用IComparer对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     }

      在功能页面要对上述类实例化

     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     }

      可以看出ArrayList类型有一个公开的接口(用于排序Sort),传入的参数是实现IComparer接口的一个实例,正好我们上面定义的ComparerName1(根据产品的名字来排序),那么在C#2中又是如何实现?

    正如前面所说C#2引入泛型的概念(对应的产品类为Product2类),定义一个实现IComparer<Product2>接口的类即可

    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     }

      在功能页面使用方法和C#1一样,主要区别在于ComparerName1中需要将Object类型强制转换成Product1类型,而在使用泛型的情况下,因为已经知道了具体的类型,则避免了强制转换带来的性能损耗

      C#3中的自动属性对于排序没有作用,但是可以使用引入的Lambda表达式对排序代码的进一步精简。

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             //在C#3中使用Lambda表达式进行排序
     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     }

      Lambda表达式的实现其实是委托,用于委托实现也是一样的。

      下面来说下对于查询、打印的实现。

    C#1

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             ArrayList list = Product1.GetProducts();
     6             /*
     7                 C#1使用查询、测试、打印
     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     }

      C#2中的查询实现可以使用委托

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             List<Product2> list = Product2.GetProducts();
     6 
     7             //C#2使用匿名方法创建委托
     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     }

      结果和C#1中是一样的,打印价格大于2产品的名称,到了C#3则更精简了,因为有了Lambda表达式

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             List<Product3> list = Product3.GetProducts();
     6 
     7             //C#3中使用Lambda表达式进行查询
     8             list.FindAll(x => x.Price > 2).ForEach(x => Console.WriteLine(x.Name));
     9 
    10             Console.ReadKey();
    11 
    12         }
    13     }

      写到这里,我们有理由相信,Lambda表达式就是变相的委托,则可以引入一个想法,在使用委托的时候均可以使用Lambda表达式替代。great!!!

      请斧正。

  • 相关阅读:
    java 数组
    数组(二)
    JVM内存分配策略
    JVM垃圾收集算法
    LINUX 查看硬件配置命令
    遗传算法
    svn简单使用
    Several concepts in Data Mining
    JVM判断对象存活的算法
    JVM运行时数据区
  • 原文地址:https://www.cnblogs.com/a2htray/p/4181369.html
Copyright © 2020-2023  润新知