When using certain APIs, some parameters are declared as out
parameters.
A good example of this is the Int32.TryParse(string, out int)
method.
So let’s check the difference in invocation between C# 6.0 and C# 7.0
C# 6.0
1 | public void DoSomething(string parameter) |
C# 7.0 (with type inference)
1 | public void DoSomething(string parameter) |
The difference
Now you don’t need to define the variable on a separate row. You can inline it directly and, in fact, you could just use var
instead of int
in the previous example since it can infer the type directly inline. This is called type inference.
It is important to note however that the variable is scoped on the method and not the if
itself. So the result
parameter is available in both the if
and the else
scope.
Are you going to use it?
When new features are introduced in a language, I like to ask people whether it’s a feature they would use.
So please leave me a comment and let me know if you are going to use inline out variables.