关于C#高阶知识捡漏


高手掠过!仅仅是整理一下

自动属性:  C#自动属性可以避免原来这样我们手工声明一个私有成员变量以及编写get/set逻辑

代码如下

//Demo:

    public class User

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

    }

匿名类:

l将一组只读属性封装到单个对象中,而无需首先显式定义一个类型。 类型名由编译器生成,并且不能在源代码级使用。 代码如下: //

var v = new { Amount = 108, Message = "Hello" };

对象初始化器,集合初始化器:

代码如下:

List people = new List{

            new Person { FirstName = "Scott", LastName = "Guthrie", Age = 32 },

            new Person { FirstName = "Bill", LastName = "Gates", test02 = 85},

            new Person { FirstName = "Susanne", Age = 32 },

            null,

        };

Person person = new Person() { FirstName = “Scott”, LastName = “Guthrie”, test02 = 56 };

扩展方法:

 1 namespace ConsoleApplication2
 2 {
 3     public static class StringDemo
 4     {
 5         /// 
 6         /// 扩展方法  
 7         /// 
 8         /// 
 9         /// 要传入的数据
10         /// 
11         public static string strSet(this string txt, string str)
12         {
13             ///默认 扩展方法的使用必须是 同一 命名空间下
14             ///注意扩展方法  满足 静态类 静态方法
15             return str;
16 
17         }
18     }
19     public class Demo
20     {
21         public string Demostring()
22         {
23             string str="Demmo";
24             return str.strSet(str);
25         }
26     }
27 }

 Lambda表达式:

在介绍lamada表达式之前,先要说一下委托:

委托是一个类型,指向一个方法的指针(安全的函数指针)
代码如下:

 1  /// 
 2     /// delegate 编译时生成一个class
 3     /// 
 4     /// 
 5     /// 
 6     /// 
 7     public delegate string number(string num1, string num2);
 8     public class Demo
 9     {
10         public string test()
11         {
12             string number1 = "number1";
13             string number2 = "number2";
14             //以下会逐过程演化  直至 Lambda
15             //方法一
16             //number lo = new number(Demostring); 
17             //方法二
18             //number lo = delegate(string num1, string num2) { return num1 + num2; };
19             //方法三
20             //number lo=(string num1, string num2)=>{return num1 + num2;};   //  => 等于 goes to
21             //方法四
22             number lo = (num1, num2) => { return num1 + num2; };
23             return lo(number1, number2);
24 
25             //如果需要自定义委托,推荐使用 系统委托
26             //最常用的委托区别
27             Func<>  //具有返回值
28 
29             Action<>//没有返回值
30 
31 
32         }
33         public string Demostring(string number1, string number2)
34         {
35             return number1 + number2;
36         }
37 
38     }

 几种系统委托的详解:

<>里的out代表函数返回值类型;

<>里的in 代表函数的输入参数类型;

System.Func 代表有返回类型的委托 public delegate TResult  Func(); public delegate TResult  Func(T arg);   注:输入泛型参数-in 最多16个,输出泛型参数 -out 只有一个。 System.Action 代表无返回类型的委托 public delegate void Action(T obj);    //list.Foreach public delegate void Action(T1 arg1, T2 arg2);   注:参数最多16个 System.Predicate 代表返回bool类型的委托   - 用作执行表达式 public delegate bool Predicate(T obj);  //list.Find System.Comparison 代表返回int类型的委托  - 用作比较两个参数的大小 public delegate int Comparison(T x, T y); //list.Sort