In C#, you can define default parameters as described here. I was playing around with tuples and C#7 (using LinqPad with Preferences -> Queries -> Use Roslyn assemblies
turned on) as follows:
void Main()
{
var result=AddTuples((1, 2), (3, 4));
Console.WriteLine($"Result is: {result}");
}
(int x, int y) AddTuples((int x, int y) a, (int x, int y) b)
{
return (a.x + b.x, a.y + b.y);
}
This works fine, it shows the sum of a and b:
Result is: (4, 6)
Now I was trying to modify AddTuples
to have default parameters, but somehow I couldn't figure out how. What I tried is something like:
(int x, int y) AddTuples2((int x, int y) a = (0, 0), (int x, int y) b = (0, 0))
{
return (a.x + b.x, a.y + b.y);
}
But I am getting the error message:
CS1736 Default parameter value for 'a' must be a compile-time constant
CS1736 Default parameter value for 'b' must be a compile-time constant
(try it online with DotNetFiddle)
What am I doing wrong?
Thank you for the great answers provided, I've learned a lot. Let me wrap up: To have default values for tuple parameters, there are 3 possible options:
params
array to allow more than 2 tuples in the method's parameters, provide defaults in method bodyNote: Options 1., 2. and 4. allow to specify any desired default value, while option 3. is limited to the default values provided by the default
keyword.
The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.
There are no default parameters in C. One way you can get by this is to pass in NULL pointers and then set the values to the default if NULL is passed.
It is called default function parameters . It allows formal parameters to be initialized with default values if no value or undefined is passed.
D. No parameter of a function can be default.
a
and b
are not constants. Everything that creates a new instance (whether it is a struct or a class) is not considered a constant, and method signatures only allow constants as default values.
That said, there is no way to default tuples from the method signature, you have to create a separate method.
The only way out seems to be using nullable arguments:
(int x, int y) AddTuples2((int x, int y)? a = null, (int x, int y)? b = null)
{
(int x, int y) aNN = a ?? (0,0);
(int x, int y) bNN = b ?? (0,0);
return (aNN.x + bNN.x, aNN.y + bNN.y);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With