C # 7.3 new features

C # 7.3 version has two major themes. The first topic provides the performance and unsafe code performance as good as the security code feature. The second topic provides incremental improvements to existing features. In addition, this release adds new compiler options.

The following new features to support the security code to get a better performance of the theme:

  • It can be fixed without having to access a fixed field.
  • You can reallocate  ref local variables.
  • You can use  stackalloc the initial value of the setting item array.
  • You can use any type of support mode  fixed statements.
  • You can use other generic constraints.

To existing features the following enhancements:

  • Tuples can be used to test  == and  !=.
  • You can use a variable expression in multiple locations.
  • Properties can be attached to the property field support automatic.
  • in method of distinguishing parameter analysis has been improved.
  • Overload resolution ambiguity situation is now becoming less.

The new compiler options are:

  • -publicsignFor enabling open source software (OSS) assembly signed.
  • -pathmapFor providing the source directory mapping.
01 to enable more efficient security code
You should be able to write unsafe code safety performance and good C # code is the same. Security code error class can be avoided, such as buffer overflows, and other stray pointer memory access errors. These new features extend the functionality verifiable security code. Trying to write more secure code uses structure. These features make it easier to implement.
1.1 fixed index field is not required to fix
Definition of a structure
unsafe struct S
{
    public fixed int myFixedField[10];
}

In earlier versions of C #, you need to access a fixed variable belonging to  myFixedField one of integers. Now, the code is compiled, without variable  p fixed to a single  fixed statement:

class C
{
    static S s = new S();

    unsafe public void M()
    {
        int p = s.myFixedField[5];
    }
}

Variable  p access  myFixedField a element. No need to declare a separate  int* variable. Please note that you still need  unsafe context. In earlier versions of C #, we declare a second fixed pointers:

class C
{
    static S s = new S();

    unsafe public void M()
    {
        fixed (int* ptr = s.myFixedField)
        {
            int p = ptr[5];
        }
    }
}

For more information, please refer to the  fixed statement of the article.

1.2 may be reassigned ref local variables
Now, for  ref the local variables are initialized, it may be reassigned to refer to different instances. Now compile the following code:
REF VeryLargeStruct refLocal = REF veryLargeStruct; // initialize 
refLocal = REF anotherVeryLargeStruct;               // reallocating, reflecting different memory references.

For more information, please refer to the  ref return and  ref local variables , and  foreach articles.

1.3 stackalloc support an array initializer
When you set the initial value of the values ​​of the elements in the array, you have you can specify the value:
was arr =   new  int [ 3 ] { 1 , 2 , 3 };
was Arr 2 = new  int [] { 1 , 2 , 3 };

Now, you can use to  stackalloc perform the same array The declaration syntax:

int* pArr = stackalloc int[3] {1, 2, 3};
int* pArr2 = stackalloc int[] {1, 2, 3};
Span<int> arr = stackalloc [] {1, 2, 3};

For more information, please refer to the stackallocoperator the article.

1.4 supports more types of fixed statement

fixed Statement supports a limited set of types. Starting C # 7.3, includes any return  ref T or  ref readonly Tthe  GetPinnableReference() type of the method are likely to  fixedAdding this feature means that  fixed with  System.Span <T>  with the use and related types.

For more information, see the Language Reference  fixed Statement article.

1.5 Enhanced generic constraints

Now, the type can be  System.Enum  or  System.Delegate  base class is specified for the constraint parameter type.

Now you can use the new  unmanaged constraint parameter to specify the type must be "unmanaged type." "Unmanaged type" is not a reference type, and does not contain any reference type at any level of nesting.

For more information, please refer to the  where generic constraints and constraint types parameters of the article.

These constraints will add to the existing types are incompatible changeClosed generic type constraints may no longer meet these new requirements.

02 to enhance the existing functionality
The following features provide improvements to the language functions. These features enhance the efficiency in the preparation of C #.
2.1 yuan group support == and! =
C # tuple type is now supported  == and  !=For more information, please refer to the tuple converting a text of Equation portion.
2.2 will add features to auto-implemented properties support field
Now supports this syntax:
[field: SomeThingAboutFieldAttribute]
public int SomeProperty { get; set; }

Property  SomeThingAboutFieldAttribute used in compiler-generated  SomeProperty support field. For more information, see C # Programming Guide properties .

Method 2.3 in overloading resolution properties decisive
Add  in parameter modifier, these two methods will lead to ambiguity:
static void M(S arg);
static void M(in S arg);

Now, a read-only version of the referenced by overloading better than by a reload value (a first preceding example) is. To use read-only reference parameter called version, to be added before calling the method  in modifier.

For more information, please refer to the  in parameter modifier article.

2.4 Extended expression variable initializer in
Has been added to allow in C # 7.0  out syntax for variable declaration has been extended to include a field initializer, attribute initializer, a constructor initializer and query clauses. It allows the use of the code shown in the following example:
public class B
{
   public B(int i, out int j)
   {
      j = i;
   }
}

public class D : B
{
   public D(int i) : base(i, out var j)
   {
      Console.WriteLine($"The value of 'j' is {j}");
   }
}
2.5 Improved heavy duty candidates

In each version, the rules of overload resolution has been updated to resolve ambiguities method calls with the "obvious" choice situation. This release adds three new rules to help the compiler choose the obvious choice:

  1. When the process instance and the group contains static members, if the method in the absence of the receiver or context instance is invoked, the compiler discards instance members. If the method is invoked in the case of containing instances of the receiver, the compiler discards the static member. In the absence of the receiver, the compiler will add only static context of a static member, otherwise, would add static and instance members at the same time. When the receiver is unclear or the type of example, the compiler added simultaneously both. Static Context (which implicitly  this instance receiver can not use) contains an undefined  this body member (e.g., static members), and the inability to use the  this position (e.g., field initializer and a constructor initializer) is.
  2. When a method of the type comprising a set of parameters do not satisfy certain constraints generic method thereof, the members removed from the candidate set.
  3. For group conversion method, the return type of the delegate candidate methods from the return type mismatch concentrated removed.

You will notice this change, because when you determine which method is better, you will find ambiguity overloaded methods with fewer compiler error.

03 new compiler options
The new compiler option to support new versions of C # program and DevOps program.
3.1 public or open source signatures

-publicsign The compiler option instructs the compiler to use the public key to sign the assembly. The assembly is marked as a signature, but the signature is taken from the public key. This option enables you to use the public key to sign the assembly building in open source projects.

For more information, see  -publicsign compiler options article.

3.2 pathmap

-pathmap Compiler option instructs the compiler to generate source-path environment alternative source path map. -pathmap Option controls the PDB file or as prepared by the compiler  CallerFilePathAttribute  written source path.

For more information, see  -pathmap compiler options article.

Guess you like

Origin www.cnblogs.com/SavionZhang/p/11201364.html