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?
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?
As opposed to what others in their comments said: No, there are no differences. They both produce the exact same IL.
Both
byte.TryParse(p.Value, out _);
Console.WriteLine(_);
and
byte.TryParse(p.Value, out var _);
Console.WriteLine(_);
will produce a compiler error with C#7, since _
is not intended to be used.
The usage of _
is not restricted to out parameters, but can be used for returns, too (as Evk pointed out)
byte.TryParse(p.Value, out var _); // I don't care about the out variable
_ = SomeMethod(); // I don't care about the return value
There is an excellent answer covering most things about ommitted parameters here.
Remarks: I would prefer out _
over out var _
, since there is a clear syntactic distinction between out _
and out var legalVariableName
.
EDIT
Obviously I'm not quite right here. There are some subtle differences, see Ben Voigts answer.
_ = SomeMethod()
is to explicitly express that you do not care about the result (and not acidentally forgot about it). It can be used anywhere (I mean just void Main() { _ = 123;}
compiles fine without any out _
and so on, so it's not related to the out var _
block). –
Whey out var identifier
was legal in some prerelease versions, but not in C# 6. C# 7 is the first released version that permits out variables (which you called an inline declaration) was allowed. Semi-official reference –
Schiff _
is an undeclared identifier, the second because out typename identifier
is illegal syntax. –
Schiff byte _;
. Just as whatever p
is has to be declared. I thought that was assumed, but you have a point. –
Caughey 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.
As opposed to what others in their comments said: No, there are no differences. They both produce the exact same IL.
Both
byte.TryParse(p.Value, out _);
Console.WriteLine(_);
and
byte.TryParse(p.Value, out var _);
Console.WriteLine(_);
will produce a compiler error with C#7, since _
is not intended to be used.
The usage of _
is not restricted to out parameters, but can be used for returns, too (as Evk pointed out)
byte.TryParse(p.Value, out var _); // I don't care about the out variable
_ = SomeMethod(); // I don't care about the return value
There is an excellent answer covering most things about ommitted parameters here.
Remarks: I would prefer out _
over out var _
, since there is a clear syntactic distinction between out _
and out var legalVariableName
.
EDIT
Obviously I'm not quite right here. There are some subtle differences, see Ben Voigts answer.
_ = SomeMethod()
is to explicitly express that you do not care about the result (and not acidentally forgot about it). It can be used anywhere (I mean just void Main() { _ = 123;}
compiles fine without any out _
and so on, so it's not related to the out var _
block). –
Whey out var identifier
was legal in some prerelease versions, but not in C# 6. C# 7 is the first released version that permits out variables (which you called an inline declaration) was allowed. Semi-official reference –
Schiff _
is an undeclared identifier, the second because out typename identifier
is illegal syntax. –
Schiff byte _;
. Just as whatever p
is has to be declared. I thought that was assumed, but you have a point. –
Caughey © 2022 - 2024 — McMap. All rights reserved.
_
separately before calling this, in second syntax it will be declared as well. – Cubicout
parameter, althoughref
parameters do have to be definitely assigned prior to the call. – Schiff_
are ommittedout
parameters as of C# 7. – Unmusicalout _
andout var _
(spoiler: there is none) – Unmusicalout
parameters are not needed. As of version 7, C# supports anout
parameter that is a "throw-away" parameter,_
. It will not (and cannot) be used. This is a good thing to tell your reader that the parameter is not used. – Unmusical