Enabling C# 7 Features in Visual Studio “15” Preview

Yesterday, at Build, Microsoft released the first public preview of Visual Studio “15” – the next iteration of Visual Studio.

One of the main reasons why you’d want to try it out already is to be able to use some of the heralded C# 7 features – such as binary literals, local functions or pattern matching (to name just a few).

It’s been possible to test out these features in a slightly hacky way before (see Josh’s post) – by building Roslyn from source and deploying it into VS using the CompilerExtension VSIX, but of course it’s much easier and convenient to just use C# 7 features directly in VS “15” now.

In this post I’m gonna show you how to enable the experimental C# 7 features – because they are by default not available.


Add the following conditional compilation symbols to your project: __DEMO__ and __DEMO_EXPERIMENTAL__.

Screenshot 2016-03-30 21.53.23

Enabling C# 7 features

So let’s say you want to use local functions in your C# code. You looked at the feature discussion and you wrote this bit of code:

Unfortunately, Visual Studio is going to complain with the following error message when you try to build this piece of code:

This is a compiler options and you can pass it into csc.exe command line compiler. Of course you’d likely never want to invoke it manually, but would want to use msbuild to build the entire project/solution for you, so you can pass it to the compiler through msbuild too:

This is hardly ideal though, cause what would be really good, is being able to use the features directly from Visual Studio – and have them picked up by intellisense and the F5 debugger. This is surprisingly easy – just add a conditional compilation symbols to your project: __DEMO__.

You can do that by right clicking your project > Properties > Build > Conditional compilation symbols. This automatically takes care of enabling a bunch of C# 7 features – not just local functions but also binary literals, digit separators and so on.

Enabling pattern matching

Pattern matching is probably the coolest C# 7 feature, but it actually comes in 2 flavours in Visual Studio “15”. The first (using the is operator) is enabled by the same __DEMO__ compilation symbol and allows you do the following:

There is also a version of pattern matching that uses a match keyword. That is actually covered by a separate conditional compilation symbol – __DEMO_EXPERIMENTAL__ (symbols are comma separated).

Once you add that to your project you could try out some of the experimental pattern matching code with match, for example the snippet below:

This code above is perform type matching against the aray of animals we defined upfront and is going to print:

You can read more about pattern matching in C# 7 here.

Be Sociable, Share!

  • https://github.com/bkoelman/ResharperCodeContractNullability bkoelman

    In DNX projects, you can enable this by adding to your project.json:

    “compilationOptions”: {
    “define”: [ “__DEMO__” ]

  • Pingback: Szumma #034 – 2016 14. hét | d/fuel()

  • AH

    Nice post Filip.
    Which features from this list are implemented and working fine in VS “15”?

    features/Annotated Types
    features/Nullable Reference Types
    features/local-functions [OK]
    features/patterns [OK]
    features/tuples [Error]

    it seems that only features that you mentioned in your post are working for now, right? Trying tuples i got error.

    • http://www.ThinqLinq.com/ Jim Wooley

      In the C#7 //build/ session, they mentioned that tuple support did not make it for the VS”15″ build that came out in April.

      • AH

        Very nice, thanks for reply.

      • Rasik Bihari

        That’s why I was wondering as to why tuples are not working in my build. Was about to post this question on stackoverflow.com 😛 and then I realized that I should go through the comment section :) Thanks for the clarification.