C# 7.0 Numerical literals and OUT variables

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()}");

Output:

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.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s