In C# 7 we can do like this:
byte.TryParse(p.Value, out _)
or like this
byte.TryParse(p.Value, out var _)
Are there any differences?
The difference is how they handle an existing variable named _
.
string _;
int.TryParse("123", out var _); // legal syntax for discard
int.TryParse("123", out _); // compile error, string _ is incompatible with out int parameter
or
int _;
int.TryParse("123", out var _); // discard
Console.WriteLine(_); // error: variable is not assigned
int.TryParse("123", out _); // passes existing variable byref
Console.WriteLine(_); // ok: prints "123"
The reason for this is that out _
already had a meaning, and the language maintains backward compatibility with that old meaning. But out typename identifier
is new syntax in C# 7, so there's no backward compatibility constraint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With