I need to convert a bunch of functions from C++ to C#. These have method declarations like
void foo(struct input data*, double* ret1, double *ret2)
{
// if ret1 is not NULL, return something in it
if (ret1)
{
*ret1 = SomeComplexAndExpensiveMath(data);
}
if (ret2)
{
*ret2 = MoreComplexAndExpensiveMath(data);
}
}
When I convert that to C#, out paramters would normally be the preferred choice, but passing null
to an argument declared as "out double" is not allowed:
void foo(input data, out double ret1, out double ret2)
{
if (ret1 != null) // Error.
{
// ...
}
}
Using ref double?
as parameter type also looks weird and causes additional overhead.
Is there a way I can maintain the nice simple out double
parameter type while still not computing return values the caller doesn't need? Is there a way to know that the caller did i.e. foo(input, out _, out b)
to indicate he doesn't need ret1?