New features of C # 6.0/7.0
C # 7.0 new features
- Enhanced ref
- You can pass a reference to a value variable ref int b = Ref A;
- You can return a reference to return ref X for the returned value;
- Automatically create out variables
- Enhanced tuples (requires expansion pack support, Install-package system.valuetuple)
- Named return value (int a, int b) GetValue () = (1, 2);
- Attribute-valued var data = GetValue (); (with DATA.A, data.b access)
- Direct-value (int a, int b) = GetValue (); (directly with a, B access)
- Enhanced is
- Type correct direct assignment A is int b
- Type SELECT statement switch (a) case int b: ... case string C: ...
- Enhanced type selection statement case int B when b < 0: ...
- Local function (defines a small function in the body of the function, accessible only in the body of the function)
- An enhanced function expression
- Supports destructor = = Single call or assignment statement
- Property expression Get = = variable; or set = = Variable =value?? "Default value";
- Enhanced null exception handling (support??). throw new Exception (message);)
- Enhanced asynchronous return type (VALUETASK<T>, requires expansion pack support, Install-package System.Threading.Tasks.Extensions)
- Enhanced literal notation (int a = 123_456; int b = 0xab_cd_ef; Same as non-underlined values)
- destructor deconstruct, calling method similar to tuple
C # 6.0 new features
- Enhanced Property expressions
- Read-only property public int a = 100;
- Property default value public int c {get; set;} = 100;
- Read-only properties and ReadOnly fields support assigning values in constructors
- function expression, public int add (int a, int b) = = a + b;
- NULL check operator
- Int? Age = List?. FirstOrDefault ()?. Age; Non-NULL for subsequent operations (age must be a nullable type)
- Int? Age = Users? [0]. Age; Non-null for index operation (age must be a nullable type)
- Nameof expression, gets the name of the variable, function, class
- Enhanced exception handling
- Await is supported in catch and finally statement blocks
- Catch support conditional expression, catch (Exception e) if (e.xxx = = XXX)
- String embedding value, var user = $ "{Name} {age}"
- Supports using static classes and then directly uses the methods in the class
- Object initializer with index: New dictionary<string, int> {["a"] = 1, ["b"] = 1,};
- Supports parameterless struct constructors (note that constructors are called only if the struct is obtained through new)
New features of C # 6.0/7.0