41

Let's assume I have a function with out parameter, however I do not need its value. Is there a way to pass no actual parameter if given result will be thrown away anyway?

EDIT:

Although the question has been voted to be dupe of Optional Output Parameters it only is if you look from the method creator's perspective. If you're the user of the method, you're interested not in making the parameter optional, just not using it without declaring the variable. And while this is not possible, with C# 7.0 it is possible to declare it in method call. So instead of:

int unusedValue;
TryGetValue("key", out unusedValue);

you get:

TryGetValue("key", out int unusedValue);

or even:

TryGetValue("key", out _);

This should be added as an answer, but:

  • I can't do this for this question, because it was marked as a dupe;
  • the question this question is seemed as a dupe of actually asks for a different thing.
konrad.kruczynski
  • 46,413
  • 6
  • 36
  • 47

3 Answers3

26

You cannot do this, but there's no rule saying that you have to use the value that comes back. You can simply pass a temporary variable that you never use again.

C# 4.0 allows optional parameters, but out parameters can't be optional.

EDIT: BTW, you can also overload the method:

int DoStuff()
{
    int temp;
    return DoStuff(out temp);
}

int DoStuff(out outParam)
{
    //...
}
Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
7

While you can't actually make the out parameter optional, you could simply create an overload for the function without the out parameter, which would then take away the need to create a temporary variable.

public void DoSomething(int param1, out int param2)
{
    /* Method work here */
}

public void DoSomething(int param1)
{
    int temp;
    DoSomething(param1, out temp);
}

EDIT: These days using discards for unused variables is ideal (documentation specific to out), as you communicate to yourself/other developers that you are deliberately ignoring the variable. You can also inline the unused variable. And why not, let's use lambda syntax as well.

public void DoSomething(int param1) => DoSomething(param1, out _);

This almost eliminates the need for making an overload at all that hides the out parameter, since in your calling code you can one-line the method call with out int _.

Doctor Blue
  • 3,769
  • 6
  • 40
  • 63
0

Not sure about C#, but in VB.Net you can simply pass in a constant to an output (byref) parameter. So if you have an integer, as an output parameter, you don't have to pass in an actual variable, you can just pass in 0, or any other valid integer. For Objects, including strings you can just pass in Nothing (Null in C#) and everything works fine. I'm not sure where the variable is stored, probably just on the stack as in any other parameter you pass in, and it disappears when the function exits.

Kibbee
  • 65,369
  • 27
  • 142
  • 182
  • I gave it a try, unfortunately it didn't work. `error CS1510: A ref or out argument must be an assignable variable`. – MasterMastic Jan 24 '13 at 02:55