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.
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.
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.
- using static System.Console;
- namespace UseLocalFunctions
- {
- class Program
- {
- static void Main(string[] args)
- {
- void Add(int x, int y)
- {
- WriteLine($"Sum of {x} and {y} is : {x + y}");
- }
- void Multiply(int x, int y)
- {
- WriteLine($"Multiply of {x} and {y} is : {x * y}");
- Add(30, 10);
- }
- Add(20, 50);
- Multiply(20, 50);
- }
- }
- }
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.
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.
- using System;
- using static System.Console;
- class Program {
- static void Main(string[] args) {
- string s = "26-Nov-2016";
- if (DateTime.TryParse(s, out DateTime date)) {
- WriteLine(date);
- }
- WriteLine(date);
- }
- }
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.
You can view the output directly on the console Window.
Tuple & Tuple Deconstruction
In the same way, we can also run tuple codes. Now, run the code snippet given below.
- class Program
- {
- static void Main(string[] args)
- {
-
- }
-
-
- (int price, int discount) GetPrice(int itemId)
- {
- var product = (500, 100);
- return product;
- }
- }
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.
Now, go to View menu and select “Command Palette”.
Inside the command Window, type “NugGet” and select the option NuGet Package Manager: Add Package.
Now, type “System.ValueTuple” inside the command prompt.
Now, select the appropriate version of “System.ValueTuple” e.g. in the screenshot given below, I have selected 4.3.0.
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.
- using static System.Console;
- class Program
- {
- static void Main(string[] args)
- {
- Program p = new Program();
- (int price, int discount) = p.GetPrice(1);
- WriteLine($" Price: {price}/- \n Discount: {discount}/-");
- }
-
-
- (int price, int discount) GetPrice(int itemId)
- {
- var product = (500, 100);
- return product;
- }
- }
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.
- using static System.Console;
- class Program
- {
- static void Main(string[] args)
- {
-
- var myData = "Custom Data";
- var myData2 = myData is string ? "String" : "Not a string";
- var myData3 = myData is string a ? a : "Not a String";
- WriteLine(myData2);
- WriteLine(myData3);
-
-
- var x = 10;
- dynamic y = 0b1001;
- var sum = y is int ? $"{y * x}" : "Invalid data";
- WriteLine(sum);
- }
-
- }
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,