Curiosity is bliss · Julien Couvreur's programming blog and more

Curiosity is bliss    Archive    Feed    About    Search

Julien Couvreur's programming blog and more

Using C# 7.1

C# 7.0 was released as part of Visual Studio 2017 (version 15.0). While we work on C# 8.0, we will also ship features that are ready earlier as point releases.

C# 7.1 is the first such release. It will ship along with Visual Studio 2017 version 15.3. To try it out today, you can install Visual Studio Preview side-by-side, quickly and safely.

As you start using new C# 7.1 features in your code, a lightbulb will offer you to upgrade your project, either to “C# 7.1” or “latest”. If you leave your project’s language version set to “default”, you can only use C# 7.0 features (“default” means the latest major version, so does not include point releases).

LangVer7_1.png

Here are more specific instructions for using C# 7.1 in ASP.NET and ASP.NET Core and .NET CLI. The NuGet compiler packages for this release are versioned 2.3.

You can provide feedback on the C# features on the Roslyn repository or via the “Report a Problem” button in Visual Studio.

C# 7.1 features

In addition to numerous issues fixed in this release, the compiler comes with the following features for C# 7.1 (summarized below): async Main, pattern-matching with generics, “default” expressions, and inferred tuple names.

You can find more details about C# 7.1 and our progress on C# 7.2 and 8.0 in the language feature status page.

Async Main

This makes it easier to get started with async code, by recognizing static async Task Main() {...await some asynchronous code...} as a valid entry-point to your program.

Pattern-matching with generics

This allows using open types in type patterns. For example, case T t:.

“default” literal

This lets you omit the type in the default operator (default(T)) when the type can be inferred from the context. For instance, you can invoke void M(ImmutableArray<int> x) with M(default), or specify a default parameter value when declaring void M(CancellationToken x = default).

DefaultError.png

DefaultLightbulb.png

Inferred tuple names

This is a refinement on tuple literals (introduced in 7.0) which makes tuple element names redundant when they can be infered from the expressions. Instead of writing var tuple = (a: this.a, b: X.Y.b), you can simply write var tuple = (this.a, X.Y.b);. The elements tuple.a and tuple.b will still be recognized.

InferredTupleNameLightbulb.png

ValueTuple availability on various platforms

As part of VS2017, we have just released C# and VB support for tuples, which Mads describes in the C# 7.0 announcement post.

Under the covers, C#/VB tuples and corresponding F# 4.1 “struct tuples” are lowered into ValueTuple types of various arities and nesting, and tuple element names are stored in TupleElementNamesAttribute. Vlad describes both in some details in “How tuples relate to ValueTuple” and “More about tuple element names”.

Since the early prototyping work for tuples, we not only focused on language questions, but more generally the end-to-end experience of tuples. Central to that experience is how to make the ValueTuple types available.

Without those types, the compilation will fail and reports error CS8179: Predefined type 'System.ValueTuple`2' is not defined or imported.

In order to maximize scenarios were you can use tuples, we took a two-pronged approach:

  1. provide System.ValueTuple nuget package with support with existing frameworks,
  2. migrate ValueTuple and other types into core libraries as updated frameworks ship.

Naturally, a common question is: how soon can I use tuples without referencing this additional ValueTuple package?

Here is the latest status on migrating ValueTuple types into frameworks (as of April 2017) and the planned shipping vehicles (to the best of my knowledge):

  Version that includes ValueTuple
Full/desktop framework .NET Framework 4.7 and Windows 10 Creators Edition Update (RS2)
Core 2.0 (with planned preview in Q2 2017, release in Q3, see roadmap)
Mono Mono 5.0
.Net Standard .Net Standard 2.0

For older frameworks, the ValueTuple package should help fill the gap, including targets for netstandard1.0 and portable-net40+sl4+win8+wp8 (for Portable Class Libraries). I will keep the package updated as the migration into core libraries progresses, to provide as smooth and transparent an experience as possible.

This picture is further complicated as the ValueTuple types receive some minor updates (such as binary serializability). Such improvements will not be available to users of the ValueTuple package; they will only be implemented in frameworks themselves.

I keep track of all the library work related to ValueTuple types in this work items list. But if you have a specific question, it is probably easier to ask me directly in the comments section. Bug reports should go in the corefx github repo.

Known issues:

  • Degraded QuickWatch experience when debugging an application compiled on the full framework 4.6 or earlier on a machine with 4.7 installed. See issue details. This will be mitigated in first quarterly release of VS2017 and expected to be fully fixed in 4.7.1.

Bacteria Evolving

The evolution of a bacteria, illustrated with an 11 days time-lapse. The bands have increasingly concentrated antibiotics toward the middle.

The-Evolution-of-Bacteria-on-a-Mega-Plate-Petri-Dish2.gif

More info here.