Search code examples
c#parametersnullable

optional/null-able OUT parameter in C#


I have a method that has several overrides. In one of the more expanded overrides, I want to return an OUT parameter but not in my simpler overrides. For example:

public bool IsPossible(string param1, int param2)
public bool IsPossible(string param1, int param2, out bool param3)

The way I am currently achieving this, is like so:

public bool IsPossible(string param1, int param2) {
    bool temp;
    return IsPossible(param1, param2, out temp);
}

Is there a better way to achieve this? Can I (or should I) use a null-able out parameter?


Solution

  • That looks fine to me. A out cannot be optional for technical reasons (it needs to point to a valid instance).