Wednesday, 7 December 2016

Hands on CSharp 7.0 features in VS 2017 RC

As of now me and you might have seen a lot of blog posts, articles, StackOverflow Documentation and from many other sources 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 these features are still under discussion with C# design teams.
In this blogs 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 you machine then make sure you have installed the .Net core and Asp.net core tooling. If not you can again launch the setup and choose those module to install.
Now to quickly get started You can download the sample from Code.Msdn.Microsoft.com. This sample have all the feature well documented and a sample project which you can start with for hands on excercise.
Now since I have this project downloaded so when I’m opening it with VS2017 RC and trying to build it getting error:

image
To fix it you can open the Nuget package and try to get the latest Stable version i.e. 4.3.0 at this moment.

image

After update try to build again. Unfortunately I wasn’t able to build it on my machine and got below error similar to the previous one.
image

Now in order to get things working go to New Project in the same solution and Create a new .Net core console app.
SNAGHTML16e24a79

Restore the Nuget package for System.ValueTuple v 4.3.0 version in the newly created project:
image
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 file with same name already exist.
noProgramCSFile
But it’s not there, Possibly hidden. So you can click on top corner option of SolutionExplorer to show Hidden files.
HiddenProgramCS
Now Right click on the Program.cs and select Include the file in the Project. Now that you have the consoleApp ready and Nuget package installed. Now it’s time to add those sample from the solution 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 files and “Add as Link”.
image
After adding the files now open the Program.cs file and copy the content of Program.cs from the sample project or copy below code in your Program.cs

using CS70SampleConsole;
using System;

class Program
{
    static void Main(string[] args)
    {
        OutVariablesSample.Run();
        PatternMatchSample.Run();
        TupleSample.Run();
        DeconstructionSample.Run();
        LocalFunctionsSample.Run();
        DigitSeperatorSample.Run();
        RefReturnsAndLocalsSample.Run();

        Console.Read();
    }
}
Build the new .Net core console App and run by pressing ‘F5’ and VIOLA!!
SNAGHTML16ed313e
Now you can start playing around the new features. Documented in the CodeSample as they are the only features delivered as Preview.

No comments:

Post a Comment