|
| 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 compiler options 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 now has fewer ambiguous cases. |
| 25 | + |
| 26 | +The new compiler options are: |
| 27 | + |
| 28 | +- `-publicsign` to enable Open Source Software (OSS) signing of assemblies. |
| 29 | +- `-pathmap` to provide a mapping for source directories. |
| 30 | + |
| 31 | +The remainder of this article provides details and links to learn more about each of the improvements. |
| 32 | + |
| 33 | +## Enabling more performant safe code |
| 34 | + |
| 35 | +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. |
| 36 | + |
| 37 | +### Indexing `fixed` fields does not require pinning |
| 38 | + |
| 39 | +Consider this struct: |
| 40 | + |
| 41 | +```csharp |
| 42 | +unsafe struct S |
| 43 | +{ |
| 44 | + public fixed int myFixedField[10]; |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +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 in a safe context: |
| 49 | + |
| 50 | +```csharp |
| 51 | +class C |
| 52 | +{ |
| 53 | + static S s = new S(); |
| 54 | + |
| 55 | + unsafe public void M() |
| 56 | + { |
| 57 | + int p = s.myFixedField[5]; |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +The variable `p` accesses one element in `myFixedField`. You don't need to declare a separate `int*` variable. Note that you still need an `unsafe` context. In earlier versions of C#, you need to declare a second fixed pointer: |
| 63 | + |
| 64 | +```csharp |
| 65 | +class C |
| 66 | +{ |
| 67 | + static S s = new S(); |
| 68 | + |
| 69 | + public void M() |
| 70 | + { |
| 71 | + fixed (int* ptr = s.myFixedField) |
| 72 | + { |
| 73 | + int p = ptr[5]; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Fore more information, see the article on the [`fixed` statement](../language-reference/keywords/fixed-statement.md). |
| 80 | + |
| 81 | +### `ref` local variables may be reassigned |
| 82 | + |
| 83 | +Now, `ref` locals may be reassigned to refer to different instances after being initialized. The following code now compiles: |
| 84 | + |
| 85 | +```csharp |
| 86 | +ref VeryLargeStruct refLocal = ref veryLargeStruct; // initialization |
| 87 | +refLocal = ref anotherVeryLargeStruct; // reassigned, refLocal refers to different storage. |
| 88 | +``` |
| 89 | + |
| 90 | +For more information, see the article on [`ref` returns and `ref` locals](../programming-guide/classes-and-structs/ref-returns.md). |
| 91 | + |
| 92 | +### `stackalloc` arrays support initializers |
| 93 | + |
| 94 | +You've been able to specify the values for elements in an array when you initialize it: |
| 95 | + |
| 96 | +```csharp |
| 97 | +var arr = new int[3] {1, 2, 3}; |
| 98 | +var arr2 = new int[] {1, 2, 3}; |
| 99 | +``` |
| 100 | + |
| 101 | +Now, that same syntax can be applied to arrays that are declared with `stackalloc`: |
| 102 | + |
| 103 | +```csharp |
| 104 | +int* pArr = stackalloc int[3] {1, 2, 3}; |
| 105 | +int* pArr2 = stackalloc int[] {1, 2, 3}; |
| 106 | +Span<int> arr = stackalloc [] {1, 2, 3}; |
| 107 | +``` |
| 108 | + |
| 109 | +For more information, see the [`stackalloc` statement](../language-reference/keywords/stackalloc.md) article in the language reference. |
| 110 | + |
| 111 | +### More types support the `fixed` statement |
| 112 | + |
| 113 | +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. |
| 114 | + |
| 115 | +For more information, see the [`fixed` statement](../language-reference/keywords/fixed-statement.md) article in the language reference. |
| 116 | + |
| 117 | +### Enhanced generic constraints |
| 118 | + |
| 119 | +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. |
| 120 | + |
| 121 | +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. |
| 122 | + |
| 123 | +For more information, see the articles on [`where` generic constraints](../language-reference/keywords/where-generic-type-constraint.md) and [constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md). |
| 124 | + |
| 125 | +## Make existing features better |
| 126 | + |
| 127 | +The second theme provides improvements to features in the language. These features improve productivity when writing C#. |
| 128 | + |
| 129 | +### Tuples support `==` and `!=` |
| 130 | + |
| 131 | +The C# tuple types now support `==` and `!=`. Fore more information, see the section covering [equality](../tuples.md#equality-and-tuples) in the article on [tuples](../tuples.md). |
| 132 | + |
| 133 | +### Attach attributes to the backing fields for auto-implemented properties |
| 134 | + |
| 135 | +This syntax is now supported: |
| 136 | + |
| 137 | +```csharp |
| 138 | +[field: SomeThingAboutFieldAttribute] |
| 139 | +public int SomeProperty { get; set; } |
| 140 | +``` |
| 141 | + |
| 142 | +The attribute `SomeThingAboutFieldAttribute` is applied to the compiler generated backing field for `SomeProperty`. For more information, see [attributes](../programming-guide/concepts/attributes/index.md) in the C# programming guide. |
| 143 | + |
| 144 | +### `in` method overload resolution tiebreaker |
| 145 | + |
| 146 | +When the `in` argument modifier was added, these two methods would cause an ambiguity: |
| 147 | + |
| 148 | +```csharp |
| 149 | +static void M(S arg); |
| 150 | +static void M(in S arg); |
| 151 | +``` |
| 152 | + |
| 153 | +Now, the by value (first in the preceding example) overload is better than the by readonly reference version. To call the version with the readonly reference argument, you must include the `in` modifier when calling the method. |
| 154 | + |
| 155 | +> [!NOTE] |
| 156 | +> This was implemented as a bug fix. This no longer is ambiguous even with the language version set to "7.2". |
| 157 | +
|
| 158 | +For more information, see the article on the [`in` parameter modifier](../language-reference/keywords/in-parameter-modifier.md). |
| 159 | + |
| 160 | +### Extend expression variables in initializers |
| 161 | + |
| 162 | +The syntax added in C# 7.0 to allow `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: |
| 163 | + |
| 164 | +```csharp |
| 165 | +public class B |
| 166 | +{ |
| 167 | + public B(int i, out int j) |
| 168 | + { |
| 169 | + j = i; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +public class D : B |
| 174 | +{ |
| 175 | + public D(int i) : B(i, out var j) |
| 176 | + { |
| 177 | + Console.WriteLine($"The value of 'j' is {j}"); |
| 178 | + } |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +### Improved overload candidates |
| 183 | + |
| 184 | +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: |
| 185 | + |
| 186 | +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. |
| 187 | +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. |
| 188 | +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. |
| 189 | + |
| 190 | +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. |
| 191 | + |
| 192 | +## New compiler options |
| 193 | + |
| 194 | +New compiler options support new build and DevOps scenarios for C# programs. |
| 195 | + |
| 196 | +### Public or Open Source signing |
| 197 | + |
| 198 | +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. |
| 199 | + |
| 200 | +For more information, see the [-publicsign compiler option](../language-reference/compiler-options/publicsign-compiler-option.md) article. |
| 201 | + |
| 202 | +### pathmap |
| 203 | + |
| 204 | +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>. |
| 205 | + |
| 206 | +For more information, see the [-pathmap compiler option](../language-reference/compiler-options/pathmap-compiler-option.md) article. |
0 commit comments