C# 7.0 New Features

Introduction

Compared to other versions of C#, C# 7.0 has some good features, I am going to explain some of them.

  • Declaring Out Variable insideTryParse.
  • Creating a function inside a function (local functions).
  • More readability in declaring complex numbers and Binary literals.
  • Return a reference of variable from a function and modify the referenced variable.
  • Simplified Tuple
Explanation 1

Declaring Out Variable inside TryParse. In earlier versions of c# we used to declare out variable outside as shown below,

But in C# 7.0 had change, now we can add this declaration inside TryParse itself, by this we can reduce the code

 
  1. static void Main(string[] args)  
  2.    {  
  3.        bool output = int.TryParse("123"out int i);  
  4.        Console.WriteLine(output);  
  5.        bool output = int.TryParse("two"out int i);  
  6.        Console.WriteLine(output);  
  7.        Console.Read();  
  8.    }   
Creating a function inside a function (local functions)

In older versions of C# we can have a function inside a function by using lambda expression or using delegate or using anonymous method but that was very complicated to read. C# 7.0 has got one cool feature, that is we can create functions inside a function which are more readable, the below code is an example of creating functions inside a function in C# 7.0

 
  1. static void Main(string[] args)  
  2.    {  
  3.        int subtract;  
  4.        int Add(int a, int b)  
  5.        {  
  6.            int difference(int c, int d)  
  7.            {  
  8.                return c - d;  
  9.            }  
  10.            subtract = difference(a, b);  
  11.            return a + b;  
  12.        }  
  13.        int result = Add(25, 15);  
  14.        Console.WriteLine("sum = "+ result);  
  15.        Console.WriteLine("difference = " + subtract);  
  16.        Console.ReadLine();  
  17.     }  

As you can see here in the above example, inside the main function I have created one function called Add(), and inside this Add() function I have created difference() function, wow this is possible, this is a cool feature isn’t it?

More readability in declaring complex numbers and Binary literals

In C# 7.0 we can separate numbers or literals by using underscore( “_” called as an under bar) , examples are shown below

 
  1. static void Main(string[] args)  
  2.    {  
  3.        decimal hugenumber = 048_____78_____8960_500_653_80;  
  4.        Console.WriteLine("Big Number\n..........................");  
  5.        Console.WriteLine(hugenumber);  
  6.   
  7.        int[] numbers = { 0b0000, 0b1_001_000_000 };  
  8.        Console.WriteLine("Binary literals from array are \n..........................");  
  9.        foreach (var item in numbers)  
  10.        {  
  11.            Console.WriteLine(item);  
  12.        }  
  13.        Console.Read();  
  14.     }  

As you can see in the above example the number 4878896050065380  was not readable, but in code declaration we can make this readable by separating parts using “-”. we can use any number of “-” , and the compiler will just ignore this when executing the program and it just gives the normal number as shown above.

Return a reference of variable from a function and modify the referenced variable

Using C# 7.0 we can get the reference of items based on index then we can modify that item using this reference. Code is as shown below,

 
  1. static void Main(string[] args)  
  2.   {  
  3.       int[] _numbers = { 10, 20, 30, 40, 50 };  
  4.       ref int refNumber = ref GetRefernce(_numbers,3)  
  5.       Console.WriteLine("Before changing third Value");  
  6.       Console.WriteLine(_numbers);  
  7.       refNumber = 25;  
  8.       Console.WriteLine("After changing third Value");  
  9.       Console.WriteLine(_numbers[3]);  
  10.       Console.Read();  
  11.    }  
  12.   public static ref int GetRefernce(int[] _array, int index)  
  13.   {  
  14.       return ref _array[index];  
  15.   } 
Simplified Tuple

in C# 7.0, Tuple is more simplified, In older versions of C# to use Tuple, we need to use keyword Tuple to declare Tuple and when retrieving time we will not know which one we are retrieving, that is we cannot give names to the variable inside Tuple, but in C# this is all  possible, no need to use keyword Tuple to declare Tuple and we can give names to the variables

Example as shown below

  
  1. static void Main(string[] args)  
  2.   {  
  3.       Tuple<intstring> ob = new Tuple<intstring>(100, "Ravi");  
  4.       Console.WriteLine("Employee Name: "+ob.Item2);  
  5.   
  6.       (intstring) ob1 = (20, "Ravi");  
  7.       Console.WriteLine("Employee Name: " + ob1.Item2);  
  8.   
  9.       (int id, string name) ob2 = (20, "Ravi");  
  10.       Console.WriteLine("Employee Name: " + ob2.name);  
  11.   
  12.       var ob3 = (Empid:20, EmployeeName: "Ravi");  
  13.       Console.WriteLine("Employee Name: " + ob3.EmployeeName);  
  14.       Console.Read();  
  15.  }

Up Next
    Ebook Download
    View all
    Learn
    View all