|
| 1 | +--- |
| 2 | +title: What's new in C# 7.3 |
| 3 | +description: An overview of new features in C# 7.3 |
| 4 | +ms.date: 05/16/2018 |
| 5 | +--- |
| 6 | +# What's new in C# 7.3 |
| 7 | + |
| 8 | +There are two main themes to the C# 7.3 release. One theme provides features that enable safe code to be as performant as unsafe code. The second theme provides incremental improvements to existing features. In addition, new compilers were added in this release. |
| 9 | + |
| 10 | +The following new features support the theme of better performance for safe code: |
| 11 | + |
| 12 | +- You can access fixed fields without pinning. |
| 13 | +- You can reassign `ref` local variables. |
| 14 | +- You can use initializers on `stackalloc` arrays. |
| 15 | +- You can use `fixed` statements with any type that supports a pattern. |
| 16 | +- You can use additional generic constraints. |
| 17 | + |
| 18 | +The following enhancements were made to existing features: |
| 19 | + |
| 20 | +- You can test `==` and `!=` with tuple types. |
| 21 | +- You can use expression variables in more locations. |
| 22 | +- You may attach attributes to the backing field of auto implemented properties. |
| 23 | +- Method resolution when arguments differ by `in` has been improved. |
| 24 | +- Overload resolution new has fewer ambiguous cases. |
| 25 | + |
| 26 | +The new compiler options are: |
| 27 | + |
| 28 | +- `deterministic` to ensure that subsequent compilations of the same source generate the same binary output. |
| 29 | +- `publicsign` to enable OSS signing of assemblies. |
| 30 | +- `pathmap` to provide a mapping for source directories. |
| 31 | + |
| 32 | +The remainder of this article provides details and links to learn more about each of the improvements. |
| 33 | + |
| 34 | +## Enabling more performant safe code |
| 35 | + |
| 36 | +You should be able to write C# code safely that performs as well as unsafe code. Safe code avoids classes of errors, such as buffer overruns, stray pointers, and other memory access errors. These new features expand the capabilities of verifiable safe code. Strive to write more of your code using safe constructs. These features make that easier. |
| 37 | + |
| 38 | +### Indexing `fixed` fields does not require pinning |
| 39 | + |
| 40 | +Consider this struct: |
| 41 | + |
| 42 | +```csharp |
| 43 | +unsafe struct S |
| 44 | +{ |
| 45 | + public fixed int myFixedField[10]; |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +In earlier versions of C#, you needed to pin a variable to access one of the integers that are part of `myFixedField`. Now, the following code compiles: |
| 50 | + |
| 51 | +```csharp |
| 52 | +var s = new S(); |
| 53 | +int p = s.myFixedField[5]; |
| 54 | +``` |
| 55 | + |
| 56 | +The variable `p` doesn't need to be pinned. Note that you still need an `unsafe` context. |
| 57 | + |
| 58 | +You can learn more in the article on the [`fixed` statement](../language-reference/keywords/fixed-statement.md). |
| 59 | + |
| 60 | +### `ref` local variables may be reassigned |
| 61 | + |
| 62 | +Now, `ref` locals may be reassigned to refer to different storage after being initialized. The following code now compiles: |
| 63 | + |
| 64 | +```csharp |
| 65 | +ref VeryLargeStruct reflocal = ref veryLargeStruct; // initialization |
| 66 | +refLocal = ref anotherVeryLargeStruct; // reassigned, refLocal refers to different storage. |
| 67 | +``` |
| 68 | + |
| 69 | +You can learn more in the article on [`ref` returns and `ref` locals](../programming-guide/classes-and-structs/ref-returns.md). |
| 70 | + |
| 71 | +### `stackalloc` arrays support initializers |
| 72 | + |
| 73 | +You have been able to specify the values for elements in an array when you initialize the array: |
| 74 | + |
| 75 | +```csharp |
| 76 | +var arr = new int[3] {1, 2, 3}; |
| 77 | +var arr2 = new int[] { 1, 2, 3}; |
| 78 | +``` |
| 79 | + |
| 80 | +Now, that same syntax can be applied to arrays that are declared with `stackalloc`: |
| 81 | + |
| 82 | +```csharp |
| 83 | +int* pArr = stackalloc int[3] {1, 2, 3}; |
| 84 | +int* pArr2 = stackalloc int[] { 1, 2, 3}; |
| 85 | +``` |
| 86 | + |
| 87 | +You can learn more in the article on the [`stackalloc` statement](../language-reference/keywords/stackalloc.md). |
| 88 | + |
| 89 | +### More types support the `fixed` statement |
| 90 | + |
| 91 | +The `fixed` statement supported a limited set of types. Starting with C# 7.3, any type that contains a `DangerousGetPinnableReference()` method that returns a `ref T` or `ref readonly T` may be `fixed`. Adding this feature means that `fixed` can be used with <xref:System.Span%601?displayProperty=nameWithType> and related types. |
| 92 | + |
| 93 | +You can learn more in the article on the [`fixed` statement](../language-reference/keywords/fixed-statement.md). |
| 94 | + |
| 95 | +### Enhanced generic constraints |
| 96 | + |
| 97 | +You can now specify the type <xref:System.Enum?displayProperty=nameWithType> or <xref:System.Delegate?displayProperty=nameWithType> as base class constraints for a type parameter. |
| 98 | + |
| 99 | +You can also use the new `unmanaged` constraint, to specify that a type parameter must be an **unmanaged type**. An **unmanaged type** is a type that isn't a reference type, and doesn't contain any reference type at any level of nesting. |
| 100 | + |
| 101 | +You can learn more in the articles on [`where` generic constraints](../language-reference/keywords/where-generic-type-constraints.md) and the article covering [constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md). |
| 102 | + |
| 103 | +## Make existing features better |
| 104 | + |
| 105 | +The second theme provides improvements to features in the language. These features improve productivity when writing C#. |
| 106 | + |
| 107 | +### Tuples support `==` and `!=` |
| 108 | + |
| 109 | +The C# tuple types now support `==` and `!=`. You can learn more about the rules in the article on [tuples](../tuples.md). |
| 110 | + |
| 111 | +### Attach attributes to the backing fields for auto-implemented properties |
| 112 | + |
| 113 | +This syntax is now supported: |
| 114 | + |
| 115 | +```csharp |
| 116 | +[field: SomeThingAboutFieldAttribute] |
| 117 | +public int SomeProperty { get; set; } |
| 118 | +``` |
| 119 | + |
| 120 | +The attribute `SomeThingAboutFieldAttribute` is applied to the compiler generated backing field for `SomeProperty`. You can learn more in the articles on [attributes in C#](../programming-guide/concepts/attributes/index.md). |
| 121 | + |
| 122 | +### `in` method overload resolution tiebreaker |
| 123 | + |
| 124 | +When the `in` argument modifier was added, these two methods would cause an ambiguity: |
| 125 | + |
| 126 | +```csharp |
| 127 | +static void M(S arg); |
| 128 | +static void M(in S arg); |
| 129 | +``` |
| 130 | + |
| 131 | +Now, the by value (first in the preceding example) overload is better than the by readonly reference version. To specify that by readonly reference version, you must include the `in` modifier when calling the method. |
| 132 | + |
| 133 | +For more information, see the article on the [`in` parameter modifier](../language-reference/keywords/in-parameter-modifier.md). |
| 134 | + |
| 135 | +### Extend expression variables in initializers |
| 136 | + |
| 137 | +The syntax added in C# 7.0 to permit out variable declarations has been extended to include field initializers, property initializers, constructor initializers, and query clauses. It enables code such as the following example: |
| 138 | + |
| 139 | +```csharp |
| 140 | +public class B |
| 141 | +{ |
| 142 | + public B(int i, out int j) |
| 143 | + { |
| 144 | + j = i; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +public class D : B |
| 149 | +{ |
| 150 | + public D(int i) : B(i, out var j) |
| 151 | + { |
| 152 | + Console.WriteLine($"The value of 'j' is {j}"); |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### Improved overload candidates |
| 158 | + |
| 159 | +In every release, the overload resolution rules get updated to address situations where ambiguous method invocations have an "obvious" choice. This release adds three new rules to help the compiler pick the obvious choice: |
| 160 | + |
| 161 | +1. When a method group contains both instance and static members, the compiler discards the instance members if the method was invoked without an instance receiver or context. The compiler discards the static members if the method was invoked with an instance receiver. When there is no receiver, the compiler includes only static members in a static context, otherwise both static and instance members. When the receiver is ambiguously an instance or type, the compiler includes both. A static context, where an implicit `this` instance receiver cannot be used, includes the body of members where no `this` is defined, such as static members, as well as places where `this` cannot be used, such as field initializers and constructor-initializers. |
| 162 | +1. When a method group contains some generic methods whose type arguments do not satisfy their constraints, these members are removed from the candidate set. |
| 163 | +1. For a method group conversion, candidate methods whose return type doesn't match up with the delegate's return type are removed from the set. |
| 164 | + |
| 165 | +You'll only notice this change because you'll find fewer compiler errors for ambiguous method overloads when you are sure which method is better. |
| 166 | + |
| 167 | +## New compiler options |
| 168 | + |
| 169 | +New compiler options support new build and DevOps scenarios for C# programs |
| 170 | + |
| 171 | +### Deterministic compiler output |
| 172 | + |
| 173 | +The `-deterministic` option instructs the compiler to produce a byte-for-byte identical output assembly for successive compilations of the same source files. |
| 174 | + |
| 175 | +By default, every compilation unique output on each compilation. The compiler adds a timestamp, and a GUID generated from random numbers. You use this option if you want to compare the byte-for-byte output to ensure consistency across builds. |
| 176 | + |
| 177 | +For more information, see the [deterministic compiler option](../language-reference/compiler-options/deterministic-compiler-option.md) article. |
| 178 | + |
| 179 | +### Public or OSS signing |
| 180 | + |
| 181 | +The `-publicsign` compiler option instructs the compiler to sign the assembly using a public key. The assembly is marked as signed, but the signature is taken from the public key. This option enables you to build signed assemblies from open-source projects using a public key. |
| 182 | + |
| 183 | +For more information, see the [publicsign compiler option](../language-reference/compiler-options/publicsign-compiler-option.md) article. |
| 184 | + |
| 185 | +### pathmap |
| 186 | + |
| 187 | +The `-pathmap` compiler option instructs the compiler to replace source paths from the build environment with mapped source paths. The pathmap option controls the source path written by the compiler to PDB files or for the <xref:System.Runtime.CompilerServices.CallerFilePathAttribute>. |
| 188 | + |
| 189 | +For more information, see the [pathmap compiler option](../language-reference/compiler-options/pathmap-compile-option.md) article. |
0 commit comments