Search code examples
c#arraysnullable

Nullable short and array


I currently got something like this

public string MyFunction()
{
   return MyFunction(null);
}
public string MyFunction(short? variable)
{
   do something if null
   else do something else
   return string;
}

Now I'm trying to make something like

public string MyFunction(short[] variable)
{
   string a;
   foreach(var z in variable)
   a = a +" "+ MyFunction(z);
}

but i recive error

The call is ambiguous between the following methods or properties

Is it even possible to stay with only one parameter cause I know that makin' function with two params will resolve problem but still I will be using only one param. It's also impossible to replace null with chosen number (e.g. 0).


Solution

  • You can use a cast to resolve the ambiguity:

    return MyFunction((short?)null);
    

    Without the cast there is no way for the compiler to detect which function you want to invoke.