|
| 1 | +--- |
| 2 | +title: What's new in C# 7.1 |
| 3 | +description: An overview of new features in C# 7.1. |
| 4 | +keywords: C# language design, 7.1, Visual Studio 2017, |
| 5 | +author: billwagner |
| 6 | +ms.author: wiwagn |
| 7 | +ms.date: 08/16/2017 |
| 8 | +ms.topic: article |
| 9 | +ms.prod: .net |
| 10 | +ms.devlang: devlang-csharp |
| 11 | +--- |
| 12 | + |
| 13 | +# What's new in C# 7.1 |
| 14 | + |
| 15 | +C# 7.1 is the first point release to the C# Language. It marks an increased |
| 16 | +release cadence for the language. You can use the new features sooner, ideally |
| 17 | +when each new feature is ready. C# 7.1 adds the ability to configure |
| 18 | +the compiler to match a specified version of the language. That enables you to |
| 19 | +separate the decision to upgrade tools from the decision to upgrade language |
| 20 | +versions. |
| 21 | + |
| 22 | +C# 7.1 adds the [language version selection](#language-version-selection) |
| 23 | +configuration element, three new language features and new compiler behavior. |
| 24 | + |
| 25 | +The new language features in this release are: |
| 26 | + |
| 27 | +* [`async` `Main` method](#async-main) |
| 28 | + - The entry point for an application can have the `async` modifier. |
| 29 | +* [`default` literal expressions](#default-literal-expressions) |
| 30 | + - You can use default literal expressions in default value expressions when the target type can be inferred. |
| 31 | +* [Inferred tuple element names](#inferred-tuple-element-names) |
| 32 | + - The names of tuple elements can be inferred from tuple initialization in many cases. |
| 33 | + |
| 34 | +Finally, the compiler has two options `/refout` and `/refonly` that |
| 35 | +control [reference assembly generation](#reference-assembly-generation). |
| 36 | + |
| 37 | +## Language version selection |
| 38 | + |
| 39 | +The C# compiler supports C# 7.1 starting with Visual Studio 2017 version 15.3, or |
| 40 | +the .NET Core SDK 2.0. However, the 7.1 features are turned |
| 41 | +off by default. To enable the 7.1 features, you need to change the language |
| 42 | +version setting for your project. |
| 43 | + |
| 44 | +In Visual Studio, right-click on the project node in Solution Explorer and select |
| 45 | +**Properties**. Select the **Build** tab and select the **Advanced** button. In the dropdown, |
| 46 | +select **C# latest minor version (latest)**, or the specific version **C# 7.1** |
| 47 | +as shown in the image following. The `latest` value means you want to use the latest |
| 48 | +minor version on the current machine. The `C# 7.1` means that you want to use C# 7.1, |
| 49 | +even after newer minor versions are released. |
| 50 | + |
| 51 | +[Setting the language version](./csharp-7-1/media/advanced-build-settings.png) |
| 52 | + |
| 53 | +Alternatively, you can edit the "csproj" file and add or modify the |
| 54 | +following lines: |
| 55 | + |
| 56 | +```xml |
| 57 | +<PropertyGroup> |
| 58 | + <LangVersion>latest</LangVersion> |
| 59 | +</PropertyGroup> |
| 60 | +``` |
| 61 | + |
| 62 | +> [!NOTE] |
| 63 | +> If you use the Visual Studio IDE to update your csproj files, the IDE |
| 64 | +> creates separate nodes for each build configuration. You'll typically |
| 65 | +> set the value the same in all build configurations, but you need to |
| 66 | +> set it explicitly for each build configuration, or select "All Configurations" |
| 67 | +> when you modify this setting. You'll see the following in your csproj file: |
| 68 | +
|
| 69 | +```xml |
| 70 | +<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> |
| 71 | + <LangVersion>latest</LangVersion> |
| 72 | +</PropertyGroup> |
| 73 | + |
| 74 | +<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> |
| 75 | + <LangVersion>latest</LangVersion> |
| 76 | +</PropertyGroup> |
| 77 | +``` |
| 78 | + |
| 79 | +Valid settings for the `LangVersion` element are: |
| 80 | + |
| 81 | +* `ISO-1` |
| 82 | +* `ISO-2` |
| 83 | +* `3` |
| 84 | +* `4` |
| 85 | +* `5` |
| 86 | +* `6` |
| 87 | +* `7` |
| 88 | +* `7.1` |
| 89 | +* `default` |
| 90 | +* `latest` |
| 91 | + |
| 92 | +The special strings `default` and `latest` resolve to the latest major |
| 93 | +and minor language versions installed on the build machine, respectively. |
| 94 | + |
| 95 | +This setting decouples installing new versions of the SDK and tools |
| 96 | +in your development environment from choosing to incorporate new language |
| 97 | +features in a project. You can install the latest SDK and tools on your |
| 98 | +build machine. Each project can be configured to use a specific version |
| 99 | +of the language for its build. |
| 100 | + |
| 101 | +## Async main |
| 102 | + |
| 103 | +An *async main* method enables you to use `await` in your `Main` method. |
| 104 | +Previously you would need to write: |
| 105 | + |
| 106 | +```csharp |
| 107 | +static int Main() |
| 108 | +{ |
| 109 | + return DoAsyncWork().GetAwaiter().GetResult(); |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +You can now write: |
| 114 | + |
| 115 | +```csharp |
| 116 | +static async Task<int> Main() |
| 117 | +{ |
| 118 | + // This could also be replaced with the body |
| 119 | + // DoAsyncWork, including its await expressions: |
| 120 | + return await DoAsyncWork(); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +If your program doesn't return an exit code, you can declare a `Main` method |
| 125 | +that returns a <xref:System.Threading.Tasks.Task>: |
| 126 | + |
| 127 | +```csharp |
| 128 | +static async Task Main() |
| 129 | +{ |
| 130 | + await SomeAsyncMethod(); |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +You can read more about the details in the |
| 135 | +[async main](../programming-guid/main-and-command-args/index.md) topic |
| 136 | +in the programming guide. |
| 137 | + |
| 138 | +## Default literal expressions |
| 139 | + |
| 140 | +Default literal expressions are an enhancement to default value expressions. |
| 141 | +These expressions initialize a variable to the default value. Where you previously |
| 142 | +would write: |
| 143 | + |
| 144 | +```csharp |
| 145 | +Func<string, bool> whereClause = default(Func<string, bool>); |
| 146 | +``` |
| 147 | + |
| 148 | +You can now omit the type on the right-hand side of the initialization: |
| 149 | + |
| 150 | +```csharp |
| 151 | +Func<string, bool> whereClause = default; |
| 152 | +``` |
| 153 | + |
| 154 | +You can learn more about this enhancement in the C# Programming Guide topic |
| 155 | +on [default value expressions](../programming-guide/statements-expressions-operators/default-value-expressions.md). |
| 156 | + |
| 157 | +This enhancement also changes some of the parsing rules for the [default keyword](../language-reference/keywords/default.md). |
| 158 | + |
| 159 | +## Inferred tuple element names |
| 160 | + |
| 161 | +This feature is a small enhancement to the tuples feature introduced in |
| 162 | +C# 7.0. Many times when you initialize a tuple, the variables used for the |
| 163 | +right side of the assignment are the same as the names you'd like for the |
| 164 | +tuple elements: |
| 165 | + |
| 166 | +```csharp |
| 167 | +int count = 5; |
| 168 | +string label = "Colors used in the map"; |
| 169 | +var pair = (count: count, label: label); |
| 170 | +``` |
| 171 | + |
| 172 | +The names of tuple elements can be inferred from the variables used to initialize |
| 173 | +the tuple in C# 7.1: |
| 174 | + |
| 175 | +```csharp |
| 176 | +int count = 5; |
| 177 | +string label = "Colors used in the map"; |
| 178 | +var pair = (count, label); // element names are "count" and "label" |
| 179 | +``` |
| 180 | + |
| 181 | +You can learn more about this feature in the [Tuples](../tuples.md)) topic. |
| 182 | + |
| 183 | +## Reference assembly generation |
| 184 | + |
| 185 | +There are two new compiler options that generate *reference-only assemblies*: |
| 186 | +[/refout](../language-reference/compiler-options/refout-compiler-option.md) |
| 187 | +and [/refonly](../language-reference/compiler-options/refonly-compiler-option.md). |
| 188 | +The linked topics explain these options and reference assemblies in more detail. |
0 commit comments