C# 7.0 Features — Let’s explore

Dipak Gawade
.Net Programming
Published in
5 min readMay 12, 2020

Although C# 7.0 was released in March 2017, there were many features of this version I was unaware of. After amazing experience with C# 6.0, I was waiting to explore C# 7.0 features as well. However, I was also waiting to get at least one problem statement to switch to C# 7.0. Fortunately, I came across such issue and I decided to explore the efficient way to handle that.

The problem was the API I was using required 3 ‘out’ parameters and every time I had to access that API method, I had to first declare 3 variables above the method call and then send them to method. That was 4 lines of code!!

Can it be reduced to 1 line, please? Yes, using C# 7.0's inline out variables declaration feature.

Lets explore it and other fascinating features of C# 7.0.

Before we proceed further, check if you have C# 7.0 language version enabled in Visual studio. To do that, open project properties -> Build -> Look for ‘Advanced’ button in right bottom corner and click it. You will see following dialog box-

Select C# 7.x version and we are good to go!!!!

Note — In this article, we will explore these features with simplest examples and then we will discuss their practical usage.

  1. Out variable declaration

In following example, I have passed ‘out’ parameter with method. To pass the parameter, I need to first declare it on the line above. So I have added 2 lines of code to achieve successful method call. Is it possible to have a variable declaration and method call in single statement?

Out before 7.0

C# 7.0 has feature in which you can declare parameter while passing it as ‘out’ argument of method. This has reduced the lines of code by 1 in this case. However, for every additional ‘out’ parameter, we save 1 more line.

Out + Variable declaration — 7.0

2. Tuple as a method return type

Tuples are already part of C# family. Many times we have seen code like ‘result.Item1’ and ‘result.Item2’ while processing tuples.

C# 7.0 has facilitated us with tuples as method return types. So we need not add many out parameters to method as before when only one return value was allowed.

However, tuples as a return type are not available in base packages. Due to this you will get following error while using tuples.

We need to add Nuget package to enable tuple functionality in project.

After the Nuget package is added, we are ready to use it in the project.

Consider I am writing a method which returns ‘division operation’ output and remainder. Now, since only one value can be returned, I need to return another output value through ‘out’ parameter as shown below.

There is nothing wrong in above code. The caller still gets both the results. However, what if, the caller wants to save those parameters together in the list? In that case, caller needs to fetch one from returned result, another from out parameter and then combine them. Is there an efficient way to get them both combined in result itself as that makes more sense?

Using tuples in return type, we can return any number of result parameters and fetch them as required. We need to take care of method return type and send as many parameters as requested. For any difference in number of parameters, the compiler shows error.

3. Discards

In above scenario, we used both the returned results further to print. What if we have a case where we don’t need to store all the returned parameters?

Suppose, the method returns 5 parameters. To use them, I need to write

(int a, int b, int c, int d, int e) = Method();

I want to use value of ‘e’ only. However, I had to define all the 4 other variables unnecessarily. To save our efforts, C# 7.0 has come up with feature called ‘Discards’. It is as simple as using ‘-’(under-score) at the place of variables which are not going to be used. So I will rewrite above statement as-

(_, _, _, _, int e) = Method();

It saved me from deciding 4 other variable names, which was waste effort!!

4. Throws in expression

I am not sure when I am going to use this feature but I liked the concept. It is usual to throw exception for invalid values or arguments. However, can we write a single expression which either works on valid value or throws exception? No.

Below is the example in which we are trying to assign age, only if input value is greater than 0, else we want to throw an exception. I wanted to use tertiary operator here but since there is ‘throw exception’ part, I switched to ‘if’ :(

Using C# 7.0, I don’t have to work that way now. I can use tertiary operator to fulfill both the requirements in single statement.

Note — Due to length of this article, I have to break this article in parts. Other parts have equally exciting features to discuss. Continue reading!!!

--

--

Dipak Gawade
.Net Programming

Software geek, blogger. Passionate about learning new concepts and share knowledge. Believe in 'lifetime understanding' than 'interview prep'.