Return multiple values to a method caller

前端 未结 26 2295
故里飘歌
故里飘歌 2020-11-21 21:57

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?

相关标签:
26条回答
  • 2020-11-21 22:31

    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 :)

    0 讨论(0)
  • 2020-11-21 22:32

    From this article, you can use three options as posts above said.

    KeyValuePair is quickest way.

    out is at the second.

    Tuple is the slowest.

    Anyway, this is depend on what is the best for your scenario.

    0 讨论(0)
  • 2020-11-21 22:33

    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 Tuple<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.

    0 讨论(0)
  • 2020-11-21 22:33

    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.

    0 讨论(0)
  • 2020-11-21 22:35

    In C# 7 and above, see this answer.

    In previous versions, you can use .NET 4.0+'s Tuple:

    For Example:

    public Tuple<int, int> GetMultipleValue()
    {
         return Tuple.Create(1,2);
    }
    

    Tuples with two values have Item1 and Item2 as properties.

    0 讨论(0)
  • 2020-11-21 22:36

    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.

    0 讨论(0)
提交回复
热议问题