Skip to content

Commit d1dcc14

Browse files
committed
create "what's new in C# 7.3"
Fixes #5289 This PR adds the overview to "What's new in C# 7.3". I updated the wording in the TOC to remove the repeated "What's new" phrasing. /cc @jcouv @MadsTorgersen /cc @Welchen This PR relies on new files added in PR #5408
1 parent 1aa1f3c commit d1dcc14

File tree

3 files changed

+220
-44
lines changed

3 files changed

+220
-44
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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.

docs/csharp/whats-new/index.md

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,38 @@ the C# language. The following links provide detailed information on the
1212
major features added in each release.
1313

1414
> [!IMPORTANT]
15-
> The C# language relies on types and methods in a *standard library* for some of the features. One example is exception processing. Every `throw` statement or expression is checked to ensure the object being thrown is derived from <xref:System.Exception>. Similarly, every `catch` is checked to ensure that the type being caught is derived from <xref:System.Exception>. Each version may add new requirements. To use the latest language features in older environments, you may need to install specific libraries. These dependencies are documented in the page for each specific version. You can learn more about the [relationships between language and library](relationships-between-language-and-library.md) for background on this dependency.
16-
15+
> The C# language relies on types and methods in a *standard library* for some of the features. One example is exception processing. Every `throw` statement or expression is checked to ensure the object being thrown is derived from <xref:System.Exception>. Similarly, every `catch` is checked to ensure that the type being caught is derived from <xref:System.Exception>. Each version may add new requirements. To use the latest language features in older environments, you may need to install specific libraries. These dependencies are documented in the page for each specific version. You can learn more about the [relationships between language and library](relationships-between-language-and-library.md) for background on this dependency.
1716
17+
* [C# 7.3](csharp-7-3.md):
18+
- This page describes the latest features in the C# language. C# 7.3 is currently available in [Visual Studio 2017 version 15.7](https://www.visualstudio.com/vs/whatsnew/), and in the [.NET Core 2.1 SDK Preview](../../core/whats-new/index.md).
1819
* [C# 7.2](csharp-7-2.md):
19-
- This page describes the latest features in the C# language. C# 7.2 is currently available in [Visual Studio 2017 version 15.5](https://www.visualstudio.com/vs/whatsnew/), and in the [.NET Core 2.0 SDK](../../core/whats-new/index.md).
20-
20+
- This page describes the latest features in the C# language. C# 7.2 is currently available in [Visual Studio 2017 version 15.5](https://www.visualstudio.com/vs/whatsnew/), and in the [.NET Core 2.0 SDK](../../core/whats-new/index.md).
2121
* [C# 7.1](csharp-7-1.md):
22-
- This page describes the features in C# 7.1. These features were added in [Visual Studio 2017 version 15.3](https://www.visualstudio.com/vs/whatsnew/), and in the [.NET Core 2.0 SDK](../../core/whats-new/index.md).
23-
22+
- This page describes the features in C# 7.1. These features were added in [Visual Studio 2017 version 15.3](https://www.visualstudio.com/vs/whatsnew/), and in the [.NET Core 2.0 SDK](../../core/whats-new/index.md).
2423
* [C# 7.0](csharp-7.md):
25-
- This page describes the features added in C# 7.0. These features were added in [Visual Studio 2017](https://www.visualstudio.com/vs/whatsnew/) and [.NET Core 1.0](../../core/whats-new/index.md) and later
26-
24+
- This page describes the features added in C# 7.0. These features were added in [Visual Studio 2017](https://www.visualstudio.com/vs/whatsnew/) and [.NET Core 1.0](../../core/whats-new/index.md) and later
2725
* [C# 6](csharp-6.md):
28-
- This page describes the features that were added in C# 6. These features are available in Visual Studio 2015 for Windows developers, and on .NET Core 1.0 for developers exploring C# on macOS and Linux.
29-
30-
<!--* [C# Interactive](../interactive/index.md):
31-
- This page describes C# Interactive, an interactive Read Eval Print Loop (REPL) that you can use to explore the C# language. You can use it to write code interactively and see it execute immediately, without any compile or build step.
32-
-->
26+
- This page describes the features that were added in C# 6. These features are available in Visual Studio 2015 for Windows developers, and on .NET Core 1.0 for developers exploring C# on macOS and Linux.
3327
* [Cross Platform Support](../../core/index.md):
34-
- C#, through .NET Core support, runs on multiple platforms. If you are interested in trying C# on macOS, or on one of the many supported Linux distributions, learn more about .NET Core.
28+
- C#, through .NET Core support, runs on multiple platforms. If you are interested in trying C# on macOS, or on one of the many supported Linux distributions, learn more about .NET Core.
29+
* [.NET Compiler Platform SDK](../roslyn-sdk/index.md):
30+
- The .NET Compiler Platform SDK enables you to write code that performs static analysis on C# code. You can use these APIs to find potential errors, or bad practices, suggest fixes, and even implement those fixes.
3531

36-
<!--
37-
- [.NET Compiler Platform SDK](../roslyn/index.md):
38-
- The .NET Compiler Platform SDK enables you to write code that performs static analysis on C# code. You can use these APIs to find potential errors, or bad practices, suggest fixes, and even implement those fixes.
39-
-->
40-
4132
## Previous Versions
42-
The following lists key features that were introduced in previous versions of the C# language and Visual Studio .NET.
43-
44-
* Visual Studio .NET 2013:
45-
- This version of Visual Studio included bug fixes, performance improvements, and technology previews of .NET Compiler Platform ("Roslyn") which became the .NET Compiler Platform SDK<!--Link to ../roslyn/index.md-->.
46-
47-
* C# 5, Visual Studio .NET 2012:
48-
- `Async` / `await`, and [caller information](../programming-guide/concepts/caller-information.md) attributes.
49-
50-
* C# 4, Visual Studio .NET 2010:
51-
- `Dynamic`, [named arguments](../programming-guide/classes-and-structs/named-and-optional-arguments.md), optional parameters, and generic [covariance and contra variance](../programming-guide/concepts/covariance-contravariance/index.md).
52-
53-
* C# 3, Visual Studio .NET 2008:
54-
- Object and collection initializers, lambda expressions, extension methods, anonymous types, automatic properties, local `var` type inference, and [Language Integrated Query (LINQ)](../programming-guide/concepts/linq/index.md).
55-
56-
* C# 2, Visual Studio .NET 2005:
57-
- Anonymous methods, generics, nullable types, iterators/yield, `static` classes, and covariance and contra variance for delegates.
58-
59-
* C# 1.1, Visual Studio .NET 2003:
60-
- `#line` pragma and xml doc comments.
6133

62-
* C# 1, Visual Studio .NET 2002:
63-
- The first release of [C#](../csharp.md).
34+
The following lists key features that were introduced in previous versions of the C# language and Visual Studio .NET.
35+
36+
* Visual Studio .NET 2013:
37+
- This version of Visual Studio included bug fixes, performance improvements, and technology previews of .NET Compiler Platform ("Roslyn") which became the [.NET Compiler Platform SDK](../roslyn-sdk/index.md).
38+
* C# 5, Visual Studio .NET 2012:
39+
- `Async` / `await`, and [caller information](../programming-guide/concepts/caller-information.md) attributes.
40+
* C# 4, Visual Studio .NET 2010:
41+
- `Dynamic`, [named arguments](../programming-guide/classes-and-structs/named-and-optional-arguments.md), optional parameters, and generic [covariance and contra variance](../programming-guide/concepts/covariance-contravariance/index.md).
42+
* C# 3, Visual Studio .NET 2008:
43+
- Object and collection initializers, lambda expressions, extension methods, anonymous types, automatic properties, local `var` type inference, and [Language Integrated Query (LINQ)](../programming-guide/concepts/linq/index.md).
44+
* C# 2, Visual Studio .NET 2005:
45+
- Anonymous methods, generics, nullable types, iterators/yield, `static` classes, and covariance and contra variance for delegates.
46+
* C# 1.1, Visual Studio .NET 2003:
47+
- `#line` pragma and xml doc comments.
48+
* C# 1, Visual Studio .NET 2002:
49+
- The first release of [C#](../csharp.md).

docs/toc.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,11 @@
215215
<!-- The "What's New" section is short, and one level
216216
deep, so leave it in the main TOC -->
217217
## [What's new in C#](csharp/whats-new/index.md)
218-
### [What's new in C# 7.2](csharp/whats-new/csharp-7-2.md)
219-
### [What's new in C# 7.1](csharp/whats-new/csharp-7-1.md)
220-
### [What's new in C# 7.0](csharp/whats-new/csharp-7.md)
221-
### [What's new in C# 6](csharp/whats-new/csharp-6.md)
218+
### [C# 7.3](csharp/whats-new/csharp-7-3.md)
219+
### [C# 7.2](csharp/whats-new/csharp-7-2.md)
220+
### [C# 7.1](csharp/whats-new/csharp-7-1.md)
221+
### [C# 7.0](csharp/whats-new/csharp-7.md)
222+
### [C# 6](csharp/whats-new/csharp-6.md)
222223
### [C# Version History](csharp/whats-new/csharp-version-history.md)
223224
### [Relationships between language and framework](csharp/whats-new/relationships-between-language-and-library.md)
224225
<!-- End What's New -->

0 commit comments

Comments
 (0)