Working With Out, Exception And Literal Enhancements In C# 7.0

I am here to continue the series related to C# 7.0 features. Today, we will be going through some of the enhancements introduced in out, exception and literals and will demonstrate their uses in an easy manner.

Let’s go to them one by one.

Enhancement in out parameter

Remember when using out parameter, its constraint of having it declared beforehand? The good news in C# 7.0 is that it’s no longer needed to be declared first and instead it can be declared in-line.

The snippet given below presents C# 6 and C# 7 versions.

  1. //In C# 6.0  
  2. string empName;  
  3. Console.WriteLine($"Using C#6 {CSharpSixOutTest("101", out empName)}");  
  4.   
  5. //In C# 7.0  
  6. Console.WriteLine($"\nUsing C#7 {CSharpSevenOutTest("101", out string empName2)}");  
  7.   
  8. public static string CSharpSixOutTest(string empId, out string empName)  
  9.         {  
  10.             //Fetch empName by passing empId  
  11.             empName = "Hello Prakash!";  
  12.             return empName;  
  13.         }  

Output

Output

As you can see that in C# 7.0, it’s no longer required to have declaration of out variable beforehand.

There is one more scenario (TryParse), where out is used often and having the declaration beforehand was the constraint; butnow this has been removed in C# 7.0.

Let’s look at the example for the same in both the versions.

  1. CSharpOutTest("101");  
  2.   
  3. public static void CSharpOutTest(string empId)  
  4.         {  
  5.             //Using C# 6.0  
  6.             int result;  
  7.             if (int.TryParse(empId, out result))  
  8.             {  
  9.                 Console.WriteLine("Parsed successfully using C#6 syntax");  
  10.             }  
  11.   
  12.             //Using C# 7.0  
  13.             if (int.TryParse(empId, out int result2))  
  14.             {  
  15.                 Console.WriteLine("Parsed successfully using C#7 syntax");  
  16.             }  
  17.         }  

Output

Output

As you can see in the code given above, C# 7.0 directly accepts the in-line declaration.

Enhancement in exception:

Can you throw an exception from practically everywhere?

Yes, you read it right. With C# 7.0, you can throw the exceptions from almost everywhere.

Let’s look at the example given below.

  1. //Exception improvements -we can throw an exception directly through expression  
  2. Console.WriteLine(GetFirstName("Prakash Tripathi"));  
  3.   
  4. public static string GetFirstName(string name)  
  5.         {  
  6.             string[] arr = name.Split(' ');  
  7.             return (arr.Length > 0) ? arr[0] : throw new Exception("Name doesn't exist!");  
  8.         }  

Output

Output

Now, let’s create an exception to test this. Just replace the 2nd line in GetFirstName method, as shown below.

  1. return (arr.Length > 0) ? arr[2] : throw new Exception("Name doesn't exist!");  

Here, the output is given below.

Output

Thus, it’s clear that now in C# 7.0, we can throw an exception from the expression as well. Isn’t that cool?

If you run the same code in the previous version, you will get an error, as shown below.

Output

Enhancement in literals

Now, let’s look at the changes introduced in literals. Basically there are two enhancements.

  • New binary literal.

    C# 7.0 also supports binary literal, which starts with 0B or 0b.

    Example

    var binLit = 0B110010; //new binary literals.

  • Support of digital separators (_)

    Now, we can use underscore (_) to make logical separation in literals.

    Important point to be noted here is that separators are only for visual purpose. When they are stored into identifiers, only real value remains.

    Example

    var decLitNew = 478_____1254_3698_44; //Underscores are now supported, value remains the same

    Now, let’s have a look at the example to understand it more.

    1. //Before C#7  
    2.             var decLit = 50; //Decimal Literal  
    3.             var hexLit = 0X32; //Hexadecimal Literal but printed in number as 1000  
    4.             Console.WriteLine($"Decimal Literal using C#6 syntax: {decLit}");  
    5.             Console.WriteLine($"Hexadecimal Literal using C#6 syntax: {hexLit}\n");  
    6.   
    7.             //New in C#7 - digit separator  
    8.             var decLitNew = 478_____1254_3698_44; //Underscores are now supported, value remains the same  
    9.             var hexLitNew = 0X3_2; //Hexadecimal Literal with _  
    10.             var binLit = 0B110010; //new binary literals  
    11.             var binLit2 = 0B110_010; //new binary literals  
    12.             Console.WriteLine($"Decimal Literal using C#7 digit separator: {decLitNew}");  
    13.             Console.WriteLine($"Hexadecimal Literal using C#7 digit separator: {hexLitNew}");  
    14.             Console.WriteLine($"Binary Literal using C#7 syntax: {binLit}");  
    15.             Console.WriteLine($"Binary Literal using C#7 digit separator: {binLit2}");  

Output

Output

As you can see in the output, while printing hexa (0X3_2) and binary (0B110_010 and 0B110_010), the literals are getting printed as decimal 50. Also, while printing, only real value is considered.

Conclusion

In this article, we have gone through and learnt the things given below.

  • Enhancements in out parameter
  • Enhancement in the exceptions
  • New binary separator and digital separator.

Hope, you have liked the article. Look forward for your comments/suggestions.

If you also want to go through the previous parts of the series, the links are given below.