Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't an out parameter have a default value?

Currently when trying to do something in a method that takes an out parameter, I need to assign the value of the out parameter in the method body, e.g.

public static void TryDoSomething(int value, out bool itWorkerd)
{
    itWorkerd = true;

    if (someFavourableCondition)
    {
        // if I didn't assign itWorked variable before this point, 
        // I get an error: "Parameter itWorked must be assigned upon exit.
        return;
    }

    // try to do thing

    itWorkerd = // success of attempt to do thing
}

I'd like to be able to set a default value of the itWorked parameter so I don't have to arbitrarily set the value in the body of the method.

public static void TryDoSomething(int value, out bool itWorkerd = true)
{
    if (someFavourableCondition)
    {
        // itWorked was already assigned with a default value
        // so no compile errors.
        return;
    }

    // try to do thing

    itWorkerd = // success of attempt to do thing
}

Why is it not possible to assign a default value for an out parameter?

like image 321
DaveDev Avatar asked May 29 '14 12:05

DaveDev


People also ask

Can out parameter have a default value?

The designers considered the feature and rejected it as not useful enough to be worth the cost of implementing. The designers considered the feature and rejected it as being confusing because it uses similar syntax to default value parameters, but has a quite different meaning.

Which parameter does not have default value out?

An IN OUT parameter cannot have a default value. An IN OUT actual parameter or argument must be a variable.

What is an out parameter?

The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition​ as well as in the calling method.

Can out parameter be optional?

When a method with an omitted optional parameter is called, a stack frame containing the values of all the parameters is created, and the missing value(s) are simply filled with the specified default values. However, an "out" parameter is a reference, not a value.


2 Answers

Default values are available for parameters passed by value. The parameter is still passed to the function but if the code omits the parameter, the compiler supplies the missing value.

Your proposed feature is quite different. Instead of the caller omitting to pass the value, you propose to allow the implementer of the function to omit setting the value. So, this is a quite different feature. Why was it not implemented? Here are some possible reasons:

  1. Nobody thought to implement this feature.
  2. The designers considered the feature and rejected it as not useful enough to be worth the cost of implementing.
  3. The designers considered the feature and rejected it as being confusing because it uses similar syntax to default value parameters, but has a quite different meaning.
like image 80
David Heffernan Avatar answered Oct 12 '22 04:10

David Heffernan


I appreciate this isn't exactly answering the original question, but I'm unable to contribute to the comments. I had the same question myself so found myself here.

Since C#7 now allows out parameters to effectively be variable declarations in the calling scope, assigning a default value would be useful.

Consider the following simple method:

private void ResolveStatusName(string statusName, out string statusCode)
    {
        statusCode = "";
        if (statusName != "Any")
        {
            statusCode = statusName.Length > 1
                ? statusName.Substring(0, 1)
                : statusName;
        }
    }

It felt intuitive to modify it like so:

 private void ResolveStatusName(string statusName, out string statusCode = "")
    {
        if (statusName != "Any")
        {
            statusCode = statusName.Length > 1
                ? statusName.Substring(0, 1)
                : statusName;
        }
    }

The intention was to not only declare the statusCode value, but also define it's default value, but the compiler does not allow it.

like image 22
Ben Wesson Avatar answered Oct 12 '22 05:10

Ben Wesson