Running C# 7 Code With Visual Studio Code Editor

c#

I have written some articles on C# 7 and following are the links for the same

Many developers were interested in running C# 7 code with Visual Studio Code Editor. Some developers want to use it on Linux or due to any other reason and I was getting queries from the developers that how to run C# 7 features with Visual Studio Code Editor. Thus, I decided to write an article for the same.

Open the command prompt and navigate to the folder, where you want to create your C# 7 project.

Type the command “dotnet new console” to create a new console Application.

c#

If .NET Core has not been installed already, then you need to install .NET Core from here and C# extension from here.

Type “code ." on the command prompt to open the newly generated project in Visual Studio Code Editor.

Type “dotnet restore” on the command prompt to restore the dependencies.

Type “dotnet run” to see the output.

c#

You can see that it is printing "Hello World!"

Now, I am going to use C# 7 features.

Local functions or the nested functions

Using nested functions inside a function is called as a local function. To understand how a local function works, just have a look at the code given below and see how you are going to write and call Add() and Multiply() methods.

Paste the code given below inside Visual Studio Code Editor. 

  1. using static System.Console;  
  2. namespace UseLocalFunctions  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             void Add(int x, int y)  
  9.             {  
  10.                 WriteLine($"Sum of {x} and {y} is : {x + y}");  
  11.             }  
  12.             void Multiply(int x, int y)  
  13.             {  
  14.                 WriteLine($"Multiply of {x} and {y} is : {x * y}");  
  15.                 Add(30, 10);  
  16.             }  
  17.             Add(20, 50);  
  18.             Multiply(20, 50);  
  19.         }  
  20.     }  
  21. }   

c#

On the command prompt, type “dotnet restore” and subsequently type “dotnet run”. It will print the output on the console Window. The screenshot is given below for the same.

c#

Inline out Variables

Before C# 7, if you had to pass a variable as out parameter, then the variable must have been declared before passing it to the method but in C# 7, you can declare variable directly when you are passing it inside a method.
c# 

  1. using System;  
  2. using static System.Console;    
  3. class Program {    
  4.     static void Main(string[] args) {    
  5.         string s = "26-Nov-2016";    
  6.         if (DateTime.TryParse(s, out DateTime date)) {    
  7.             WriteLine(date);    
  8.         }    
  9.         WriteLine(date);    
  10.     }  
  11. }    

Now, run the preceding code snippet for inline out variable declaration.

Let’s debug the code in Code Editor.

We can put the debugger in the same way as of Visual Studio, using F9.

c#

You can view the output directly on the console Window.

c# 

Tuple & Tuple Deconstruction

In the same way, we can also run tuple codes. Now, run the code snippet given below.

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.          
  6.     }  
  7.   
  8.     //returning price & discount    
  9.     (int price, int discount) GetPrice(int itemId)  
  10.     {  
  11.         var product = (500, 100);  
  12.         return product;  
  13.     }  
  14. }   

You will notice that it will not compile easily and will throw some exceptions. Thus, you need to install some package from NuGet Package Manager. If NuGet ackage Manager is not installed already with your Visual studio code, then you can install it. Following is the screenshot for the same to install NuGet Package Manager.

c#

Now, go to View menu and select “Command Palette”.

c#

Inside the command Window, type “NugGet” and select the option NuGet Package Manager: Add Package.

c#

Now, type “System.ValueTuple” inside the command prompt.

c#

c#

Now, select the appropriate version of “System.ValueTuple” e.g. in the screenshot given below, I have selected 4.3.0.

c#

Now, I am going back to the command prompt and type “dotnet restore” to restore the packages.

Now, run the code snippet given below in Code Editor. 

  1. using static System.Console;  
  2. class Program  
  3. {  
  4.     static void Main(string[] args)  
  5.     {  
  6.             Program p = new Program();  
  7.             (int price, int discount) = p.GetPrice(1);  
  8.             WriteLine($" Price: {price}/- \n Discount: {discount}/-");  
  9.     }  
  10.   
  11.     //returning price & discount    
  12.     (int price, int discount) GetPrice(int itemId)  
  13.     {  
  14.         var product = (500, 100);  
  15.         return product;  
  16.     }  
  17. }   

You may be thinking that why do you need to install System.ValueTuple from NuGet, then I would like to clarify that even if you use Visual Studio 2017, you need to download it from NuGet because these tuple syntaxes will not be supported with installing “System.ValueTuple”.

Pattern matching

In the same way, pattern matching can run with Visual Studio Code Editor. A code snippet is given below for the same. 

  1. using static System.Console;  
  2. class Program  
  3. {  
  4.     static void Main(string[] args)  
  5.     {  
  6.             //Example 1     
  7.             var myData = "Custom Data";    
  8.             var myData2 = myData is string ? "String" : "Not a string";    
  9.             var myData3 = myData is string a ? a : "Not a String";    
  10.             WriteLine(myData2);    
  11.             WriteLine(myData3);    
  12.   
  13.             //Example 2     
  14.             var x = 10;    
  15.             dynamic y = 0b1001;    
  16.             var sum = y is int ? $"{y * x}" : "Invalid data";    
  17.             WriteLine(sum);    
  18.     }  
  19.     
  20. }   

In this article, I have just explained that how you can write and execute your C# 7 code with Visual Studio Code Editor. In the same way, we can test & execute following concepts:

Local functions or nested functions, Binary Literal, Digit Separators, Pattern matching (Type Pattern, Constant Pattern, Var Pattern, Recursive Pattern, Property Pattern & Property Sub-pattern, Switch Statement, Match Expression, Case expression, Throw expression, Destructuring assignment, Testing Nullable, Arithmetic simplification, Tuple decomposition, Complex Pattern, Wildcard Pattern etc.), ref returns and ref Locals, Tuples (Tuples Enhancement), Throw Expressions, Expression Bodied Members Expression bodied Methods, Expression bodied Properties, Expression bodied constructor, Expression bodied destructor, Expression bodied getters, Expression bodied setters, Inline Out variable declaration, Tuple deconstruction.

I am not going to the explain all the concepts of C# 7 but you can find it in the article given below,


Similar Articles