Hands On C# 7.0 Features In VS 2017

As of now, you and I might have seen a lot of blog posts, articles, StackOverflow Documentation and things from many other source about the new/proposed features of C# 7.0 since the announcement in Aug 2016. I’m not going to write about the same as some are finalized but more of these features are still under discussion with C# design teams.

In this blog post I’m going to address how to start hands on with new C# 7.0 feature rolled out with Preview release.

If you have VS2017 RC installed on your machine, make sure that you have installed the .NET Core and ASP.NET core tooling. If not, you can again launch the setup and choose the module to install.

Now, to quickly get started, you can download the sample from Code.Msdn.Microsoft.com. This sample has all the features well documented and a sample project, which you can experience.

Now, I have this project downloaded, so when I’m opening it with VS2017 RC and trying to build it leads to an error.



To fix it, you can open NuGet package and try to get the latest stable version i.e. 4.3.0 at this moment.



After updating, try to build again. Unfortunately, I wasn’t able to build it on my machine and got an error, which is similar to the previous one.



In order to get things working, go to New Project in the same solution and create a new .NET Core console app.



Restore NuGet package for System.ValueTuple v 4.3.0 version in the newly created project.



Now, after installing the package, go to the Program.cs file, which isn’t in the project, so If you try to add it in the ConsoleApp, it will throw an error that a file with the same name already exists.



But it’s not there, as it is possibly hidden. Thus, you can click on the top corner option of Solution Explorer to show the hidden files.

Now, right click on the Program.cs and select Include the file in the Project. Now, you have the consoleApp ready and NuGet package is installed. Now, it’s time to add the sample from the solution, which we downloaded from Code MSDN and get the file in the new project.

Right click on the Project and Add –> Existing Items. Navigate to the directory, where the solution directory is, and open the Samples folder. Select all the files and “Add as Link”.



After adding the files, now open the Program.cs file and copy the content of Program.cs from the sample project or copy the code, mentioned below in your Program.cs.

  1. using CS70SampleConsole;  
  2. using System;  
  3. class Program  
  4. {  
  5.     static void Main(string[] args)   
  6.   {  
  7.         OutVariablesSample.Run();  
  8.         PatternMatchSample.Run();  
  9.         TupleSample.Run();  
  10.         DeconstructionSample.Run();  
  11.         LocalFunctionsSample.Run();  
  12.         DigitSeperatorSample.Run();  
  13.         RefReturnsAndLocalsSample.Run();  
  14.         Console.Read();  
  15.     }  
  16. }  
Build the new .NET Core console app and run by pressing ‘F5’ and VIOLA.



Now, you can start playing around the new features. They are documented in the CodeSample, as they are the only features delivered as Preview.

Next Recommended Readings