out var declaration
suggest changeA common pattern in C# is using bool TryParse(object input, out object value)
to safely parse objects.
The out var
declaration is a simple feature to improve readability. It allows a variable to be declared at the same time that is it passed as an out parameter.
A variable declared this way is scoped to the remainder of the body at the point in which it is declared.
Example
Using TryParse
prior to C# 7.0, you must declare a variable to receive the value before calling the function:
int value;
if (int.TryParse(input, out value))
{
Foo(value); // ok
}
else
{
Foo(value); // value is zero
}
Foo(value); // ok
In C# 7.0, you can inline the declaration of the variable passed to the out
parameter, eliminating the need for a separate variable declaration:
if (int.TryParse(input, out var value))
{
Foo(value); // ok
}
else
{
Foo(value); // value is zero
}
Foo(value); // still ok, the value in scope within the remainder of the body
If some of the parameters that a function returns in out
is not needed you can use the discard operator \_
.
p.GetCoordinates(out var x, out _); // I only care about x
An out var
declaration can be used with any existing function which already has out
parameters. The function declaration syntax remains the same, and no additional requirements are needed to make the function compatible with an out var
declaration. This feature is simply syntactic sugar.
Another feature of out var
declaration is that it can be used with anonymous types.
var a = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var groupedByMod2 = a.Select(x => new
{
Source = x,
Mod2 = x % 2
})
.GroupBy(x => x.Mod2)
.ToDictionary(g => g.Key, g => g.ToArray());
if (groupedByMod2.TryGetValue(1, out var oddElements))
{
Console.WriteLine(oddElements.Length);
}
In this code we create a Dictionary
with int
key and array of anonymous type value. In the previous version of C# it was impossible to use TryGetValue
method here since it required you to declare the out
variable (which is of anonymous type!). However, with out var
we do not need to explicitly specify the type of the out
variable.
Limitations
Note that out var declarations are of limited use in LINQ queries as expressions are interpreted as expression lambda bodies, so the scope of the introduced variables is limited to these lambdas. For example, the following code will not work:
var nums =
from item in seq
let success = int.TryParse(item, out var tmp)
select success ? tmp : 0; // Error: The name 'tmp' does not exist in the current context