C# 7.0 introduced out variables and pattern matching which allowed declaring variables inline. However, these expression variables were not permitted in field initializers, constructor initializers (this() / base()), or LINQ query clauses.
C# 7.3 removes these restrictions, allowing expression variables to be used in these additional contexts.
Code
C#
class Order
{
// Out variable in field initializer
bool valid = int.TryParse(Config.MaxItems, out var maxItems);
// Expression variable in constructor initializer
public Order(string input) : base(int.TryParse(input, out var id) ? id : 0)
{
}
}
// Pattern variable in LINQ query
var results = from s in strings
where int.TryParse(s, out var n) && n > 10
select n;C#
class Order
{
int maxItems;
bool valid;
public Order()
{
valid = int.TryParse(Config.MaxItems, out maxItems);
}
}
// Had to filter and parse separately
var results = strings
.Where(s => int.TryParse(s, out _))
.Select(s => int.Parse(s))
.Where(n => n > 10);