How to enable C# 7 in Visual Studio 2017

I was working with Visual Studio 2017 and found that 4.6.2 is not installed as default yet, so I wrote this article here about that.  In that article I also point out that you need to install the System.ValueType NuGet package to the Tuples capabilities to work.

This time I wanted to get the Pattern Matching C# 7 feature to work using this method.  Real quick the solution is the install the Microsoft.Net.Compilers package.

protected void WhichShape(object o)
{
  switch (o)
  {
   case Triangle t:
       labelTriangle.Text = ($"{t.Width} x {t.Height} x {t.Base} triangle");
       break;
   case Rectangle s when (s.Width == s.Height):
       labelSquare.Text = $"{s.Width} x {s.Height} square";
       break;
   case Rectangle r:
       labelRectangle.Text = $"{r.Width} x {r.Height} rectangle";
       break;
   case null:
       throw new ArgumentNullException(nameof(o));
   default:
       labelException.Text = "<unknown shape>";
       break;
  }
}

When I compiled an ASP.NET Application without the Microsoft.Net.Compilers installed I received the following exceptions, Figure 1:

1>------ Build started: Project: csharpseven, Configuration: Debug Any CPU ------
1>c:\users\benperk\documents\visual studio 2017\Projects\csharpseven\csharpseven\default.aspx.cs(25,31,25,32): error CS1003: Syntax error, ':' expected
1>c:\users\benperk\documents\visual studio 2017\Projects\csharpseven\csharpseven\default.aspx.cs(28,32,28,33): error CS1003: Syntax error, ':' expected
1>c:\users\benperk\documents\visual studio 2017\Projects\csharpseven\csharpseven\default.aspx.cs(28,39,28,60): error CS1528: Expected ; or = (cannot specify constructor arguments in declaration)
1>c:\users\benperk\documents\visual studio 2017\Projects\csharpseven\csharpseven\default.aspx.cs(28,39,28,40): error CS1003: Syntax error, '[' expected
1>c:\users\benperk\documents\visual studio 2017\Projects\csharpseven\csharpseven\default.aspx.cs(28,60,28,61): error CS1003: Syntax error, ']' expected
1>c:\users\benperk\documents\visual studio 2017\Projects\csharpseven\csharpseven\default.aspx.cs(28,60,28,61): error CS1002: ; expected
1>c:\users\benperk\documents\visual studio 2017\Projects\csharpseven\csharpseven\default.aspx.cs(28,60,28,61): error CS1513: } expected
1>c:\users\benperk\documents\visual studio 2017\Projects\csharpseven\csharpseven\default.aspx.cs(31,32,31,33): error CS1003: Syntax error, ':' expected
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

image

Figure 1, C# 7 compile issue, how to use C# 7

When I tried doing the same in an ASP.NET Web Site I received these exceptions, Figure 2:

CS8026 Feature 'pattern matchin' is not available in C# 5.  Please use language version 7 or greater.
CS8026 Feature 'interpolated strings' is not available in C# 5.  Please use language version 6 or greater.
CS0151 A switch expression or case label must be a bool, char, integral, enum or corresponding nullable type in C# 6 or earlier
CS8026 Feature 'nameof operator' is not available in C# 5.  Please use language version 6 or greator.

image

Figure 2, C# 7 compile issue, how to use C# 7

The solution for me was to execute the following command, Figure 3:

Install-Package Microsoft.Net.Compilers

image

Figure 3, C# 7 compile issue, how to use C# 7, solution CS8026

This command adds some configurations into the projects web.config file.  I saw some solution state that you need to update the compilerOptions to language:6 for earlier issues, but I did not need to modify this to make things work with C# 7 features.  After I installed the Microsoft.Net.Compilers package my application worked as expected, Figure 4.  Interesting that I did not need to do this to make a Console application to work…

image

Figure 3, C# 7 compile issue, how to use C# 7, solution CS0151

I read someplace that these may be included in some upcoming releases/updates.