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. //In C# 7.0
  5. Console.WriteLine($"\nUsing C#7 {CSharpSevenOutTest("101", out string empName2)}");
  6. public static string CSharpSixOutTest(string empId, out string empName)
  7. {
  8. //Fetch empName by passing empId
  9. empName = "Hello Prakash!";
  10. return empName;
  11. }

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. public static void CSharpOutTest(string empId)
  3. {
  4. //Using C# 6.0
  5. int result;
  6. if (int.TryParse(empId, out result))
  7. {
  8. Console.WriteLine("Parsed successfully using C#6 syntax");
  9. }
  10. //Using C# 7.0
  11. if (int.TryParse(empId, out int result2))
  12. {
  13. Console.WriteLine("Parsed successfully using C#7 syntax");
  14. }
  15. }

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. public static string GetFirstName(string name)
  4. {
  5. string[] arr = name.Split(' ');
  6. return (arr.Length > 0) ? arr[0] : throw new Exception("Name doesn't exist!");
  7. }

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.

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.

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.