Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't c# allow multiple return values? [closed]

Tags:

c#

I'm just wondering...Why doesn't C# allow multiple return values?

At the moment we express out intent to return multiple values via returning a class or using out - as shown below.

static void Check(MyTask task )
{
    if (GoodReasonsNotTo(task))
    {
        throw new ApplicationException("There are good reasons not to do this.");
    }
}

public static int UglyDo( MyTask task, out string response )
{
    Check(task);
    //...
    response = "Done";
    return 7;
}

static void Main(string[] args)
{
    var task = new MyTask("Add multiple return values");

    string response;
    var err = UglyDo(task, out reponse));
}

The code above could be expressed differently:

static void Check(MyTask task )
{
    if (GoodReasonsNotTo(task))
    {
        throw new ApplicationException("There are good reasons not to do this.");
    }
}

public static (int, string) PrettyDo(MyTask task)
{
    Check(task);
    //...
    return 7, "Done.";
}
static void Main(string[] args)
{
    var task = new MyTask("Add multiple return values");

    var (err, response) = PrettyDo(task); 
}

Is there anything special about return values vs function parameters? The look the same, they do the same things. Why weren't they made equal?

Also, could it support it the future?

BTW. The StackOverflow's syntax highlighter deals with it nicely - this surely means it would be a good thing.

like image 348
tymtam Avatar asked Jun 14 '26 21:06

tymtam


1 Answers

Funny that you ask. It was just announced that it will be supported in C# 7.0 [MSDN]

like image 51
Dax Fohl Avatar answered Jun 17 '26 16:06

Dax Fohl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!