Is there a specific reason why C# 7 bring inlining out
parameters but not ref
?
The following is valid on C# 7:
int.TryParse("123", out _);
But this is invalid:
public void Foo(ref int x) { }
Foo(ref _); // error
I don't see a reason why the same logic can't be applied to ref
parameters.
The reason is simple: because you're not allowed to pass an uninitialized variable into a
ref
parameter. This has always been the case, and the new syntactical sugar in C#7 doesn't change that.Observe:
The new feature in C#7 allows you to create a variable in the process of calling a method with an
out
parameter. It doesn't change the rules about uninitialized variables. The purpose of aref
parameter is to allow passing an already-initialized value into a method and (optionally) allow the original variable to be changed. The compiler semantics inside the method body treatref
parameters as initialized variables andout
parameters as uninitialized variables. And it remains that way in C#7.