C# 4.0 optional out/ref arguments

Joe Daley picture Joe Daley · May 20, 2010 · Viewed 103.7k times · Source

Does C# 4.0 allow optional out or ref arguments?

Answer

Dunc picture Dunc · Nov 30, 2011

No.

A workaround is to overload with another method that doesn't have out / ref parameters, and which just calls your current method.

public bool SomeMethod(out string input)
{
    ...
}

// new overload
public bool SomeMethod()
{
    string temp;
    return SomeMethod(out temp);
}

If you have C# 7.0, you can simplify:

// new overload
public bool SomeMethod()
{
    return SomeMethod(out _);    // declare out as an inline discard variable
}

(Thanks @Oskar / @Reiner for pointing this out.)