What are binary literals and digit separators in C# 7.0?

CsharpServer Side ProgrammingProgramming

Binary Literals

Before C# 7 we were able to assign only decimal and hexadecimal values to a variable.

In C# 7.0 binary literal has been introduced and it allows us binary value to the variable.

Digit Separator

Digit Separator takes the form of a single underscore (_). This separator can be used within any numeric literal as a means of improving legibility.

Example Binary Literals

Example

class Program{
   public static void Main(){
      var bn = 0b1000001;
      System.Console.WriteLine(bn.GetType());
      System.Console.WriteLine(Convert.ToChar(bn));
      Console.ReadLine();
   }
}

Output

System.Int32
A

Example Digit Seperator

Example

class Program{
   public static void Main(){
      long Salary = 1_00_00_00_00_000;
      System.Console.WriteLine(Salary.GetType());
      System.Console.WriteLine(Salary);
      Console.ReadLine();
   }
}

Output

System.Int64
100000000000
raja
Published on 19-Aug-2020 17:26:29
Advertisements