Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check inside method whether some optional argument was passed

How do I check if an optional argument was passed to a method?

public void ExampleMethod(int required, string optionalstr = "default string",
    int optionalint = 10)
{

    if (optionalint was passed)
       return;
}

Another approach is to use Nullable<T>.HasValue (MSDN definitions, MSDN examples):

int default_optionalint = 0;

public void ExampleMethod(int required, int? optionalint,
                            string optionalstr = "default string")
{
    int _optionalint = optionalint ?? default_optionalint;
}
like image 803
Yariv Avatar asked Jan 02 '14 16:01

Yariv


People also ask

How do you indicate optional arguments?

To indicate optional arguments, Square brackets are commonly used, and can also be used to group parameters that must be specified together. To indicate required arguments, Angled brackets are commonly used, following the same grouping conventions as square brackets.

How do you find optional arguments in Python?

You pass a function and args and kwargs to it ( inspect. getcallargs(func, /, *args, **kwds) ), it will return real method's arguments used for invocation, taking into consideration default values and other stuff.

Can we pass optional parameters in Java?

Unlike some languages such as Kotlin and Python, Java doesn't provide built-in support for optional parameter values. Callers of a method must supply all of the variables defined in the method declaration.

How optional arguments can be used within a function?

Functions with optional arguments offer more flexibility in how you can use them. You can call the function with or without the argument, and if there is no argument in the function call, then a default value is used.


2 Answers

Well, arguments are always passed. Default parameter values just ensure that the user doesn't have to explicitly specify them when calling the function.

When the compiler sees a call like this:

ExampleMethod(1);

It silently converts it to:

ExampleMethod(1, "default string", 10);

So it's not techically possible to determine if the argument was passed at run-time. The closest you could get is:

if (optionalstr == "default string")
   return;

But this would behave identically if the user called it explicitly like this:

ExampleMethod(1, "default string");

The alternative, if you really want to have different behavior depending on whether or not a parameter is provided, is to get rid of the default parameters and use overloads instead, like this:

public void ExampleMethod(int required)
{
    // optionalstr and optionalint not provided
}

public void ExampleMethod(int required, string optionalstr)
{
    // optionalint not provided
}

public void ExampleMethod(int required, string optionalstr, int optionalint)
{
    // all parameters provided
}
like image 93
p.s.w.g Avatar answered Sep 22 '22 12:09

p.s.w.g


You can't, basically. The IL generated for these calls is exactly the same:

ExampleMethod(10);
ExampleMethod(10, "default string");
ExampleMethod(10, "default string", 10);

The defaulting is performed at the call site, by the compiler.

If you really want both of those calls to be valid but distinguishable, you can just use overloading:

// optionalint removed for simplicity - you'd need four overloads rather than two
public void ExampleMethod(int required)
{
    ExampleMethodImpl(required, "default string", false);
}

public void ExampleMethod(int required, string optionalstr)
{
    ExampleMethodImpl(required, optionalstr, true);
}

private void ExampleMethodImpl(int required, int optionalstr, bool optionalPassed)
{
    // Now optionalPassed will be true when it's been passed by the caller,
    // and false when we went via the "int-only" overload
}
like image 45
Jon Skeet Avatar answered Sep 23 '22 12:09

Jon Skeet