How can I return multiple values from a function i

2018-12-31 04:42发布

I read the C++ version of this question but didn't really understand it.

Can someone please explain clearly if it can be done and how?

25条回答
旧人旧事旧时光
2楼-- · 2018-12-31 05:12

you can try this

public IEnumerable<string> Get()
 {
     return new string[] { "value1", "value2" };
 }
查看更多
其实,你不懂
3楼-- · 2018-12-31 05:13

In C# 4, you will be able to use built-in support for tuples to handle this easily.

In the meantime, there are two options.

First, you can use ref or out parameters to assign values to your parameters, which get passed back to the calling routine.

This looks like:

void myFunction(ref int setMe, out int youMustSetMe);

Second, you can wrap up your return values into a structure or class, and pass them back as members of that structure. KeyValuePair works well for 2 - for more than 2 you would need a custom class or struct.

查看更多
浪荡孟婆
4楼-- · 2018-12-31 05:13

Ways to do it:

1) KeyValuePair (Best Performance - 0.32 ns):

    KeyValuePair<int, int> Location(int p_1, int p_2, int p_3, int p_4)
    {                 
         return new KeyValuePair<int,int>(p_2 - p_1, p_4-p_3);
    }

2) Tuple - 5.40 ns:

    Tuple<int, int> Location(int p_1, int p_2, int p_3, int p_4)
    {
          return new Tuple<int, int>(p_2 - p_1, p_4-p_3);
    }

3) out (1.64 ns) or ref 4) Create your own custom class/struct

ns -> nanoseconds

Reference: multiple-return-values.

查看更多
萌妹纸的霸气范
5楼-- · 2018-12-31 05:13

Today, programmers need time & unforgotable methods. A simple, working and quick solution:

private int[] SumAndSub(int A, int B)
{
    return new[] { A + B , A - B };
}

Using it in somewhere;

int sum = SumAndSub(20, 5)[0];
int sub = SumAndSub(20, 5)[1];
查看更多
回忆,回不去的记忆
6楼-- · 2018-12-31 05:14

Now that C# 7 has been released, you can use the new included Tuples syntax

(string, string, string) LookupName(long id) // tuple return type
{
    ... // retrieve first, middle and last from data storage
    return (first, middle, last); // tuple literal
}

which could then be used like this:

var names = LookupName(id);
WriteLine($"found {names.Item1} {names.Item3}.");

You can also provide names to your elements (so they are not "Item1", "Item2" etc). You can do it by adding a name to the signature or the return methods:

(string first, string middle, string last) LookupName(long id) // tuple elements have names

or

return (first: first, middle: middle, last: last); // named tuple elements in a literal

They can also be deconstructed, which is a pretty nice new feature:

(string first, string middle, string last) = LookupName(id1); // deconstructing declaration

Check out this link to see more examples on what can be done :)

查看更多
柔情千种
7楼-- · 2018-12-31 05:14

In C#7 There is a new Tuple syntax:

static (string foo, int bar) GetTuple()
{
    return ("hello", 5);
}

You can return this as a record:

var result = GetTuple();
var foo = result.foo
// foo == "hello"

You can also use the new deconstructor syntax:

(string foo) = GetTuple();
// foo == "hello"

Be careful with serialisation however, all this is syntactic sugar - in the actual compiled code this will be a Tupel<string, int> (as per the accepted answer) with Item1 and Item2 instead of foo and bar. That means that serialisation (or deserialisation) will use those property names instead.

So, for serialisation declare a record class and return that instead.

Also new in C#7 is an improved syntax for out parameters. You can now declare the out inline, which is better suited in some contexts:

if(int.TryParse("123", out int result)) {
    // Do something with result
}

However, mostly you'll use this in .NET's own libraries, rather than in you own functions.

查看更多
登录 后发表回答