Yesterday, at Build, Microsoft released the first public preview of Visual Studio “15” – the next iteration of Visual Studio.
The Visual Studio Emulator for Android and the Windows Phone emulator run on Hyper-V and make a bunch of changes to your networking configuration that you may not want, especially if you’re using Hyper-V for running other virtual machines. The Visual Studio Emulator for Android and the Windows Phone emulator run on Hyper-V and make a bunch of changes to your networking configuration that you may not want, especially if you’re using Hyper-V for running other virtual machines.
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.
TL;DR
Add the following conditional compilation symbols to your project: __DEMO__ and __DEMO_EXPERIMENTAL__.
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:
2 4 6 8 10 12 | { { { } LocalFunction(); } |
Unfortunately, Visual Studio is going to complain with the following error message when you try to build this piece of code:
2 4 | Error CS8058 Feature'local functions'isexperimental andunsupported;use'/features:localFunctions'toenable. |
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:
2 | MSBuild.exe ConsoleApplication1.csproj/p:Features='localFunctions' |
How To Stop Output Console In Visual Studio 2017
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:
2 4 6 8 | vars=foo isstringx?x:'not a string'; // prints 'foo' // 'not a string' |
Package Manager Console In Visual Studio
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:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 | classDog:Animal publicstringBarkLikeCrazy() return'WOOF WOOF WOOF'; } classCat:Animal{} { { varanimals=newAnimal[]{newDog(),newCat(),newSwan()}; let sound=animal match( caseCatc:'meow' ) { Console.WriteLine($'{animal.Type.ToString()} - {animal.Sound}'); Console.ReadKey(); } |
This code above is perform type matching against the aray of animals we defined upfront and is going to print:
2 | ConsoleApplication1.Cat-meow |
You can read more about pattern matching in C# 7 here.