zoukankan      html  css  js  c++  java
  • C# 6.0 新特性

    nameof

        class Program
        {
            static void Main(string[] args)
            {
    
                Console.WriteLine(nameof(Student));
                Console.WriteLine(nameof(Student.A));
                Console.WriteLine(nameof(Student.B));
                Console.WriteLine(nameof(Student.C));
                Console.Read();
    
            }
    
            public class Student
            {
                public string A => "";
                public int B => 0;
                public DateTime C => DateTime.MinValue;
            }
        }

    自动属性初始化 

            public class Student
            {
                public string A { get; set; } = "";
                public int B { get; set; } = 0;
                public DateTime C { get; set; } = DateTime.MinValue;
            }

    只读属性的初始化

    public int X { get; } = 2;

    using静态成员

    关于String.Format()方法的改进

    这是经典写法

    String.Format("({0}, {1})", X, Y);

    接下来一步步简化(先将String.Format用一个$代替)

    $"({0}, {1})", X, Y);

    然后将0,1两个占位符直接换成X,Y

    $"({X}, {Y})";

    用Lambda作为函数体

            public class Student
            {
                public int X { get; } = 2;
    
                public int Y { get; set; } = 1;
    
                public override string ToString() => $"({X}, {Y})";
            }

    Lambda表达式用作属性(属性只读)

            public class Student
            {
                public int X { get; } = 2;
    
                public int Y { get; set; } = 1;
    
                public int Z => X + Y;
            }

    空值判断

            static void Main(string[] args)
            {
                List<string> list = null;
                //从这里也可以看出这种操作符的一个规则:如果对象为空,则整个表达式的值为空。
                Console.WriteLine($"{list?.Count}进来了");
                Console.Read();
            }
  • 相关阅读:
    EntityFramework系列:MySql的RowVersion
    EntityFramework系列:SQLite.CodeFirst自动生成数据库
    怎么回事呢?
    为蛇么不现实
    发布到个人主页
    作别
    budao 首页
    中午吃饱了
    作业写好了吗?
    分类
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/8618658.html
Copyright © 2011-2022 走看看