C#9.0

 

C#9.0 

 

 //对象声明
 Person person1 = new ("111", "222");//语法糖

//静态修饰
 Func<int> func = static () => 1;

//为空判断
 if (name is not null) { }

//模式运算
Func<char, Boolean> func1 = (c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';

 //原生大小的数字类型
 //引入nint,nuint,nfloat  n表示native原生,改特性允许声明一个32位或64为
 nint nintValue = 11;//在32位主机中编译,需要4个字节
 int nintValue2 = 22;//使用x64编译在64为主机编译时需要8个字节

//init
public class Demo
    {

        public string A { get; init; }

        public string B { get; init; }
    }
//初始化
 Demo _demo= new Demo(){ A = "11",  B = "222"};

//record
public record Person
    {

        public Person(string first, string last) => (FirstName, LastName) = (first, last);

        public string FirstName { get; init; }

        public string LastName { get; init; }

    }

//初始化
 Person person = new Person("123", "456");
var otherPerson = person with { FirstName = "Elva2" }; //不同的引用
var otherPerson2 = person with { };//值一样,引用不一样

///Lambda参数弃元
  Func<int, int, int> func5 = (_, b) => b * 100;
  Func<int, int, int> func6 = (a, _) => a * 100;


///枚举做基类约束
 public class GerenicInfo<T> where T : Enum 
{
}
//约束泛型类型不能为空
 public class GerenicInfo2<J> where J : unmanaged
        { }



 

C# 8.0

 
//接口定义方法

public interface IUser
    {
        void GetById(int id);
        public void GetList()
        {
            Console.WriteLine("接口可定义方法");
        }
    }

 //switch
  string   weekName = weekofDay switch
 {
            0 => "周日",
            1 => "周一",
            2 => "周二",
            3 => "周三",
            4 => "周四",
            5 => "周五",
            6 => "周六",
            _ => ""
};

//switch 元组
string Demo(string s, string s1) => (s, s1) switch
  {
            ("1", "1") => "22",
            ("2", "2") => "33",
            _ => "00/不匹配"
};

//5.静态本地函数
int iValue2 = Demo();
static int Demo()
 {
          return Add(10, 23);
          static int Add(int i, int j) => i + j;
 }

 //7.可为空引用类型
string? str = null;

 

C# 7.0

 

//out变量
int.TryParse(input, out int result); 
int.TryParse(input, out var result1);

//元组
(string First, string Two) tValue = ("One", "Two");
Console.WriteLine(tValue.First);
Console.WriteLine(tValue.Two);

 var tValue2 = (First: "One", Two: "Two");
 tValue2.First = "One2";
 Console.WriteLine(tValue2.First);
 tValue.Two = "Two2";
Console.WriteLine(tValue2.Two);

//模式匹配
 int iValue = 123;
 int sum = 234;
if (iValue is int count) //如果iValue是int类型,则赋值给count
        sum += count;

//本地函数
string lValue = LocalValue("123");
string LocalValue(string name)
{
         return name + "-----";
 }

//数字文本语法改进
 long lgValue = 100_000_000;

//switch
IEnumerable<object> iList=new List<object>()
                {

                  0,new List<int>(){ 1,2,34},
                  100,null
                };
int sum = 0;
foreach (var i in iList)
{
   switch (i)
    {

     case 0: break;
       case IEnumerable<int> inList:
                        {
                            foreach (var item in inList)
                            {
                                sum += (item > 0) ? item : 0;
                            }
                            break;
                        }

       case int n when n > 0:
         sum += n;
                        break;
       default: break;
        case null: break;

                }
}

 

C# 6.0

 

//  using static
using static System.Math;

double dValue = Math.Pow(1, 2);
double dNewValue = Pow(3, 4);//静态引入后,可直接使用方法Pow

 //NULL 条件运算符 ?.  ??
Student student = null;
String fuleName = student?.FullName;//获取属性
string ToStrValue = student?.ToString();//调用函数方法
fuleName = fuleName ?? "姓名-01";

//异常筛选器--catch() when 
try
{
   int iValue = Int32.Parse("s");
}
 catch (Exception ex) when (ex.Message.Contains("未将对象"))
{
        throw ex;
}

//Dictionary初始化
 Dictionary<int, string> keyValuePairs = new Dictionary<int, string>() {
                { 1,"12"},
                {2,"2" }
                };

keyValuePairs = new Dictionary<int, string>()
{

     [3] = "333",
     [4] = "444"
 };

 

posted @ 2021-01-27 14:34  Lin_pin  阅读(30)  评论(0编辑  收藏