Last Updated on January 30, 2021 by sandeeppote

Numeric literal syntax

Reading numbers makes it difficult to understand code. C# 7 has improvement done on the digit separator.

public static int IntegerLiterals()
 { return 1_000_000; }

public static double DoubleLiterals()
 { return 9_8_7.1_2_3; }

public static int BinaryLiterals()
 { return 0b0001_0000; }

 Console.WriteLine($" One Million : {NumericalLiterals.IntegerLiterals()}");
 Console.WriteLine($" Double : {NumericalLiterals.DoubleLiterals()}");
 Console.WriteLine($" Binary to Decimal : {NumericalLiterals.BinaryLiterals()}");


One Million : 1000000
Double : 987.123
Binary to Decimal : 16


Inline out variables

Earlier declaring out variables has to be done separately. Now this can be done in line as follows

public static int InlineOutVariables()
 string input = "100";
 if (!int.TryParse(input, out int result))
 return 0;
 return result;

You can declare out variable where you use it.  The declared out variables also leaks out of the if statement scope and used later.