Out variables
C# 7.0 has taken a fresh look at out
variables. This is a small change, but really one that improves the readability and flow of the code. Previously, we first had to declare a variable to use as an out parameter in a method. In C# 7.0 we no longer need to do that.
Getting ready
We will be using an often used method to test if a value is of a specific type. Yes, you guessed it, we're going to be using TryParse
. I can already hear some of you groan (or is it just me?). Using TryParse
is (for me anyway) such a bittersweet thing to do. It's great being able to try and parse something to test if it is valid, but the use of the out
variable was never as neat and tidy as I would have liked. If you are not familiar with the TryParse
method, it is a method that tests to see if a value parses to a specific type. If it does, TryParse
will return a Boolean value of true
; otherwise, it will return false
.
How to do it...
- The following code example will illustrate how we used to have to use
TryParse
to check if a string value is a valid integer. You will notice that we had to declare the integer variableintVal
, which was used as theout
variable. TheintVal
variable would just sort of hang there in mid air, usually not initialized and waiting to be used inTryParse
.
string sValue = "500"; int intVal; if (int.TryParse(sValue, out intVal)) { WriteLine($"{intVal} is a valid integer"); // Do something with intVal }
- In C# 7.0 this has been simplified, as can be seen in the following code example. We can now declare the
out
variable at the point where it is passed as an out parameter, like so:
if (int.TryParse(sValue, out int intVal)) { WriteLine($"{intVal} is a valid integer"); // Do something with intVal }
- This is a small change, but a very nice one. Run the console application and check the output displayed.

- As we are declaring the
out
variable as an argument to theout
parameter, the compiler will be able to infer what the type should be. This means that we can also use thevar
keyword, like this:
if (int.TryParse(sValue, out var intVal)) { WriteLine($"{intVal} is a valid integer"); // Do something with intVal }
How it works...
The changes that C# 7.0 has made to out
variables are not major. They are, however, a major convenience to those developers who use it often. So far in this chapter, we have seen the use of Tuples, pattern matching, and out
variables. We can easily combine some of what we have learned to create something truly unique. Consider the use of extension methods, Tuples, and out
variables. We can easily create an extension method called ToInt()
that has the following implementation:
public static (string originalValue, int integerValue, bool isInteger) ToInt(this string stringValue) { var t = (original: stringValue, toIntegerValue: 0, isInt: false); if (int.TryParse(stringValue, out var iValue)) { t.toIntegerValue = iValue; t.isInt = true; } return t; }
We create a Tuple literal that will be returned in the event of the TryParse
returning false. If the TryParse
is true
, I set the t.toIntegerValue
and t.isInt
values. The code that calls the extension method looks as follows:
var (original, intVal, isInteger) = sValue.ToInt(); if (isInteger) { WriteLine($"{original} is a valid integer"); // Do something with intVal }
When you run your console application, you will see that the output is exactly the same as before. This just illustrates the power of the new features in C# 7.0 when combined with each other. Throw some pattern matching into the mix, and we will have a very potent extension method. I'll leave you folks to play around with this some more. There is a lot to discover.