c# - Why ref parameters can not be ignored like out parameters? -
is there specific reason why c# 7 bring inlining out
parameters not ref
?
the following valid on c# 7:
int.tryparse("123", out _);
but invalid:
public void foo(ref int x) { } foo(ref _); // error
i don't see reason why same logic can't applied ref
parameters.
the reason simple: because you're not allowed pass uninitialized variable ref
parameter. has been case, , new syntactical sugar in c#7 doesn't change that.
observe:
int i; myoutparametermethod(out i); // allowed int j; myrefparametermethod(ref j); // compile error
the new feature in c#7 allows create variable in process of calling method out
parameter. doesn't change rules uninitialized variables. purpose of ref
parameter allow passing already-initialized value method , (optionally) allow original variable changed. compiler semantics inside method body treat ref
parameters initialized variables , out
parameters uninitialized variables. , remains way in c#7.
Comments
Post a Comment