C#语法糖个人笔记

    技术2024-01-11  102

    1. 经过简化的Property

    普通:

    private string _myName; public string MyName { get { return _myName; } set { _myName = value; } }

    语法糖:

    public string MyName { get; set; } public string MyName { get; set; } = "张三"; public string MyName { protected get; private set; }

    2. 取list中的值

    普通:

    foreach (string str in listString) { Console.WriteLine(str); }

    语法糖:

    listString.ForEach(s => Console.WriteLine(s));

    3. 字符串嵌入值

    普通:

    var str=string.Format("时间:{0}", DateTime.Now);

    语法糖:

    var str=$"时间:{DateTime.Now}";

    4. 问号(?)表达式

    4.1 可空类型修饰符(?)

    普通:

    Nullable<int> intOne = 5;

    语法糖:

    int? intTwo = 5;

    4.2 合并运算符(??)

    语法糖:当前面为空是将后面的值进行赋值

    int intA = null; int intB = intA ?? 0;

    4.3 NULL检查运算符(?.)

    语法糖:当对象为空,直接返回,不为空返回值

    string str = "abcd"; Console.WriteLine(str?.Length);// 输出 4

    4.4 "?[]"运算符

    5. 自动释放资源(using)

    普通:

    // try finally 写法 SqlConnection conn = null; try { conn = new SqlConnection("数据库连接字符串"); conn.Open(); } finally { conn.Close(); conn.Dispose(); }

    语法糖:

    using (SqlConnection conn=new SqlConnection("数据库连接字符串")) { conn.Open(); }

    6. 成员索引

    语法糖:

    public class Product { public string this[string color] { get { return String.Format("{0}经典长袖衬衫", color); } } } public static main() { Product product = new Product(); string str = product["白色"];//这里输出 白色经典长袖衬衫 }

    7. 在属性/方法中使用lambda

    语法糖:

    1 public string NameFormat => string.Format("姓名: {0}", "summit"); 2 public void Print() => Console.WriteLine(Name);

    8. 静态类的导入

    语法糖:

    using static System.Console; class Program {   static void Main(string[] args)   {     WriteLine("hello wolrd");   } }

    9.元组和弃元

    元组语法糖:

    var xs = new[] { 4, 7, 9 }; var limits = FindMinMax(xs); Console.WriteLine($"Limits of [{string.Join(" ", xs)}] are {limits.min} and {limits.max}"); // Output: // Limits of [4 7 9] are 4 and 9 var ys = new[] { -9, 0, 67, 100 }; var (minimum, maximum) = FindMinMax(ys); Console.WriteLine($"Limits of [{string.Join(" ", ys)}] are {minimum} and {maximum}"); // Output: // Limits of [-9 0 67 100] are -9 and 100 (int min, int max) FindMinMax(int[] input) { if (input is null || input.Length == 0) { throw new ArgumentException("Cannot find minimum and maximum of a null or empty array."); } var min = int.MaxValue; var max = int.MinValue; foreach (var i in input) { if (i < min) { min = i; } if (i > max) { max = i; } } return (min, max); }

    10.switch表达式

    语法糖:

    public static RGBColor FromRainbow(Rainbow colorBand) => colorBand switch { Rainbow.Red => new RGBColor(0xFF, 0x00, 0x00), Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00), Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00), Rainbow.Green => new RGBColor(0x00, 0xFF, 0x00), Rainbow.Blue => new RGBColor(0x00, 0x00, 0xFF), Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82), Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3), _ => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)), };

    元组模式:

    public static string RockPaperScissors(string first, string second) => (first, second) switch { ("rock", "paper") => "rock is covered by paper. Paper wins.", ("rock", "scissors") => "rock breaks scissors. Rock wins.", ("paper", "rock") => "paper covers rock. Paper wins.", ("paper", "scissors") => "paper is cut by scissors. Scissors wins.", ("scissors", "rock") => "scissors is broken by rock. Rock wins.", ("scissors", "paper") => "scissors cuts paper. Scissors wins.", (_, _) => "tie" };

    11.匹配模式

    语法糖:

    public static bool IsLetter(this char c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';
    Processed: 0.017, SQL: 9