Do not give argument 'out'

You can assign default values ​​to normal function arguments, so

bool SomeFunction(int arg1 = 3, int arg2 = 4)

can be invoked by any of the following methods.

bool val = SomeFunction();
bool val = SomeFunction(6);
bool val = SomeFunction(6,8);

Is there a way to do something like this (besides creating an unused variable) so that

bool SomeOtherFunction(int arg1, out int argOut)

can be called in one of the following ways:

int outarg;
bool val = SomeOtherFunction(4,outarg);
bool val = SomeOtherFunction(4);

Is this possible, or if not, what are some good workarounds? Thank.

+3
source share
4 answers

Good workaround:

bool SomeOtherFunction(int arg1, out int argOut){ ... }

bool SomeOtherFunction(int arg1)
{
    int dummyArgOut;
    return SomeOtherFunction(arg1, dummyArgOut);
}

I would even say its the best workaround.

+6
source

Yes, use an overloaded version of the same method that does not accept the second argument:

public bool SomeOtherFunction(int arg1)
{
    int ignore;
    return SomeOtherFunction(arg1, out ignore);
}
+1
source

, ref out . , . - ( ) out .

0
source

There is no default argument in C # to do a similar thing, you have to implement an overload method. Or you can use this , which you must handle with your arguments

-1
source

All Articles