Allow discards as default in out argument. #2941
Replies: 5 comments
-
Are you suggesting that optional parameters should generate overloads for the permutations of the different values that may be passed? Optional parameters don't do that today. This also implies that you'd always want to pass the parameters in a specific order and not optionally discard a parameter in the middle, which is also not the behavior of optional parameters today. For example, how could you do this? var nowTime = new NowTime();
nowTime.Deconstruct(out var hour, second: out var second); Whether or not the value is discarded is a matter for the consumer of the method, not the method itself. These methods still need to assign a valid value to that argument. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour public class Sample {
public void M1(int a=10,int b=20,int c=30,int d=40) {
}
} In the class, var nowTime = new NowTime();
nowTime.Deconstruct(out var hour, out _, out var second, out _); |
Beta Was this translation helpful? Give feedback.
-
Yes, because the optional arguments are resolved by the compiler and inserted into the method call. There are no overloads. I get that the desire here is to enable positional deconstruction using a smaller number of elements without having to write out all of the |
Beta Was this translation helpful? Give feedback.
-
I agree with @HaloFour, this doesn't seem like a straightforward use for deconstruction. @ChanyaKushima Have you seen the new property pattern in C# 8? What you would write is something like class NowTime
{
public int Hour => ...
public int Minute => ...
public int Second => ...
public int Millisecond => ...
}
object o;
if (o is NowTime { Hour: var hour, Second: var second })
{
...
} |
Beta Was this translation helpful? Give feedback.
-
I like this, prevent me having to write additional overloads for |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to use discards like following.
Example:
The above code behaves the same as below.
This syntax can use normal method.
Example:
This code is ...
This syntax is discards used optional arguments. So the following code throws compilation error.
Beta Was this translation helpful? Give feedback.
All reactions