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.
-
- string empName;
- Console.WriteLine($"Using C#6 {CSharpSixOutTest("101", out empName)}");
-
-
- Console.WriteLine($"\nUsing C#7 {CSharpSevenOutTest("101", out string empName2)}");
-
- public static string CSharpSixOutTest(string empId, out string empName)
- {
-
- empName = "Hello Prakash!";
- return empName;
- }
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.
- CSharpOutTest("101");
-
- public static void CSharpOutTest(string empId)
- {
-
- int result;
- if (int.TryParse(empId, out result))
- {
- Console.WriteLine("Parsed successfully using C#6 syntax");
- }
-
-
- if (int.TryParse(empId, out int result2))
- {
- Console.WriteLine("Parsed successfully using C#7 syntax");
- }
- }
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.
-
- Console.WriteLine(GetFirstName("Prakash Tripathi"));
-
- public static string GetFirstName(string name)
- {
- string[] arr = name.Split(' ');
- return (arr.Length > 0) ? arr[0] : throw new Exception("Name doesn't exist!");
- }
Output
Now, let’s create an exception to test this. Just replace the 2nd line in GetFirstName method, as shown below.
- return (arr.Length > 0) ? arr[2] : throw new Exception("Name doesn't exist!");
Here, the output is given below.
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.
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.
-
- var decLit = 50;
- var hexLit = 0X32;
- Console.WriteLine($"Decimal Literal using C#6 syntax: {decLit}");
- Console.WriteLine($"Hexadecimal Literal using C#6 syntax: {hexLit}\n");
-
-
- var decLitNew = 478_____1254_3698_44;
- var hexLitNew = 0X3_2;
- var binLit = 0B110010;
- var binLit2 = 0B110_010;
- Console.WriteLine($"Decimal Literal using C#7 digit separator: {decLitNew}");
- Console.WriteLine($"Hexadecimal Literal using C#7 digit separator: {hexLitNew}");
- Console.WriteLine($"Binary Literal using C#7 syntax: {binLit}");
- Console.WriteLine($"Binary Literal using C#7 digit separator: {binLit2}");
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.