You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While definite assignment rules are definitely good thing to have in C#, statements like this (to satisfy definite assignment rules) looks pretty silly in methods with "early" returns. It may be convenient to allow out parameter "initialization" with some constant default value in-place, using optional parameter syntax:
stringDoSomething(stringkey,outstringvalue=null,outintid=0){vara= F(key);if(a==null)returnnull;// ok, 'value' and 'id' are assignedvarb= G(a);if(b==null)returnnull;value= H(b,out id);return J(b);}
@ViIvanov this is declaration-site feature, just to satisfy definite assignment rules. "Default" value has nothing to do with invocations, it's should not be stored in metadata with [DefaultValue] attribute.
Agree with @svick. The risk of confusion with this syntax is pretty great, as seen by @ViIvanov's question.
I don't think syntax making it easier to add new methods that use out parameters should really be a priority, as the proposed tuples feature will be much nicer for the caller to interact with and serve the same purpose (returning multiple values).
However, as currently specified, this needs the method to be not static. So you could not use this in an extension Deconstruct method. Fortunately, with "extention everything" it wouldn't be much of an issue.
extensionclass PersonExtensions : Person
{publicvoidDeconstruct(outstringname=this.Name,outintage=this.Age){}}
This could also be used in record expansion as it is used for With method.
@controlflow commented on Thu Apr 02 2015
In methods with
out
-parameters it is very common to see first statements like:While definite assignment rules are definitely good thing to have in C#, statements like this (to satisfy definite assignment rules) looks pretty silly in methods with "early" returns. It may be convenient to allow out parameter "initialization" with some constant default value in-place, using optional parameter syntax:
@ViIvanov commented on Thu Apr 02 2015
Can I call this method like that:
? Is this side effect?
@controlflow commented on Thu Apr 02 2015
@ViIvanov this is declaration-site feature, just to satisfy definite assignment rules. "Default" value has nothing to do with invocations, it's should not be stored in metadata with
[DefaultValue]
attribute.@svick commented on Thu Apr 02 2015
I don't understand, why does the current syntax look silly?
Also, I think that using the same syntax as optional parameters without actually making the parameters optional would be confusing.
@MgSam commented on Thu Apr 02 2015
Agree with @svick. The risk of confusion with this syntax is pretty great, as seen by @ViIvanov's question.
I don't think syntax making it easier to add new methods that use out parameters should really be a priority, as the proposed tuples feature will be much nicer for the caller to interact with and serve the same purpose (returning multiple values).
@alrz commented on Thu Dec 22 2016
@gafter I think "caller-receiver parameters" from record spec draft can be applied here for a straightforward
Deconstruct
implementation.However, as currently specified, this needs the method to be not static. So you could not use this in an extension Deconstruct method. Fortunately, with "extention everything" it wouldn't be much of an issue.
This could also be used in record expansion as it is used for
With
method.@jcouv commented on Sat Oct 21 2017
Moving language design discussion over to csharplang. Thanks
The text was updated successfully, but these errors were encountered: