zoukankan      html  css  js  c++  java
  • 关于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<Person> people = new List<Person>{

                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         /// <summary>
     6         /// 扩展方法  
     7         /// </summary>
     8         /// <param name="txt"></param>
     9         /// <param name="str">要传入的数据</param>
    10         /// <returns></returns>
    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  /// <summary>
     2     /// delegate 编译时生成一个class
     3     /// </summary>
     4     /// <param name="num1"></param>
     5     /// <param name="num2"></param>
     6     /// <returns></returns>
     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<out TResult>();
    public delegate TResult  Func<in T, out TResult>(T arg);
     
    注:输入泛型参数-in 最多16个,输出泛型参数 -out 只有一个。
    System.Action 代表无返回类型的委托
    public delegate void Action<in T>(T obj);    //list.Foreach
    public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);
     
    注:参数最多16个
    System.Predicate<T> 代表返回bool类型的委托   - 用作执行表达式
    public delegate bool Predicate<in T>(T obj);  //list.Find
    System.Comparison<T> 代表返回int类型的委托  - 用作比较两个参数的大小
    public delegate int Comparison<in T>(T x, T y); //list.Sort
  • 相关阅读:
    poj 2485 Highways 最小生成树
    hdu 3415 Max Sum of MaxKsubsequence
    poj 3026 Borg Maze
    poj 2823 Sliding Window 单调队列
    poj 1258 AgriNet
    hdu 1045 Fire Net (二分图匹配)
    poj 1789 Truck History MST(最小生成树)
    fafu 1181 割点
    减肥瘦身健康秘方
    人生的问题
  • 原文地址:https://www.cnblogs.com/workcn/p/4378170.html
Copyright © 2011-2022 走看看