Binary Literal, Digit Separator, Local Functions

This article explains the new features of C# 7.0. In C# 7.0 lots of enhancements and new features have been introduced with Visual Studio 2017 which makes developers lives easy. Let's explore one by one these features with a small example which will help us to understand easily. Here, I will cover features like:

  1. Binary Literals
  2. Digit Separators
  3. Local Functions
Binary Literals

In C# 7.0 binary literal has been introduced and it allows the developer to assign a binary value to the variable. Before C# 7 we were able to assign only decimal and hexadecimal values to a variable. The first snippet is for C# 7.0. We can assign myNumber2 and type in 0b111111111111, and the compiler will not give any error. If I wanted to do this in a previous version of Visual Studio, the compiler would not understand this, as you can see an error gets thrown in snippet 2.
  1. class Program      
  2. {      
  3.         static void Main(string[] args)      
  4.         {      
  5.             int myNumber = 50;      
  6.             int myNumber1 = 0xFFDC;      
  7.             int myNumber2 = 0b111111111111;      
  8.             int digitSeparator = 1_000_000;      
  9.             int[] number = {0b1, 0b10, 0b1010, 0b1_0000 };      
  11.         }      
  12. }  

Before C# 7.0 if we tried to assign binary values to variables, the compiler will complain this is not supported.


Digit Separators

The features like binary literal are introduced in C# 7.0. It becomes a little bit tricky to read binary variables. Let's check the below example. At first glance, it is hard to read how many we have in binaryNumbervariable line no 7.

Now, we can add a digit separator, here I have used an underscore between several and now it becomes a lot more readable. Look at the below snippet line no 8 and 9, I have used _ as a digital separator.

  1. class Program      
  2. {      
  3.         static void Main(string[] args)      
  4.         {      
  5.             int myNumber = 50;      
  6.             int myNumber1 = 0xFFDC;     
  7.             int binaryNumber = 0b111111111111;   
  8.             int digitSeparator = 0b1111_1111_1111;      
  9.             int digitSeparator1 = 1_000_000;      
  10.         }      
  11. }    

Local Functions

Local function is an excellent feature added to C# 7.0. You can add local function in any method and you can call a function inside the same method and you can use a local function.

Here, I have chosen a few examples to explain local function. The first code snippet used the Factorial function as a local function which will calculate the factorial values of a given number. In this example, I have given number 5 so it will return as 120. 
  1. class Program      
  2. {      
  3.     static void Main(string[] args)      
  4.     {      
  5.             int number = 1;      
  6.             int Factorial(int n) => (n < 2) ? number :  n * Factorial(n - 1);      
  7.             Console.WriteLine($"Factorial of given number is {Factorial(5)}");      
  8.             Console.ReadLine();      
  9.      }      
  11. }    

In the below code snippet, I have written two location functions Sum() and Difference() and called indie SumAndProduct() method and returned product of both function results. 

  1. public class LocalFunctions      
  2. {      
  3.     public int SumAndProduct(int a, int b)      
  4.     {      
  5.         int Sum(int x, int y)      
  6.         {      
  7.             return x + y;      
  8.         }      
  10.         int Difference(int x, int y)      
  11.         {      
  12.             return x - y;      
  13.         }      
  15.         int sum = Sum(a, b);      
  16.         int difference = Difference(a, b);      
  18.         return sum * difference;             
  19.     }      
  20. }      

Scope of the local function
One thing to notice about the local function is that like its name, local function scope is limited to the same function of the method where it is created.

In the below code snippet, I tried to access local function GetFullName() from other function TestLocalFunctionAccess() then it gives error with red underline. 

For one more feature of C# 7.0, please visit my blog Tuples In C# 7 

Thank you again for reading. 

Jignesh Kumar
58 31.7k 2.4m
Next » Func, Action And Local Function