Skip to content

Out variables C# 7.0correctnesscode reduction

Allow out parameters to be declared inline.

When using out parameters it can be often advantageous to declare them right inline with the out parameter declaration itself. This reduces the line count, allows var to be used to infer the type from the method signature and prevents use before initialization.

Additionally C# 7.0 allows out parameters to be used in constructor initializers, query clauses, field initializers and property initializers.

Code

C#
if (int.TryParse(txtQuantity.Value, out var quantity)) {
    order.Quantity = quantity;
}
C#
int quantity;
if (int.TryParse(txtQuantity.Value, out quantity)) {
    order.Quantity = quantity;
}

More information