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

Mainly two methods are there. 1. Use out/ref parameters 2. Return an Array of objects

查看更多
长期被迫恋爱
3楼-- · 2018-12-31 05:17

Just use in OOP manner a class like this:

class div
{
    public int remainder;

    public int quotient(int dividend, int divisor)
    {
        remainder = ...;
        return ...;
    }
}

The function member returns the quotient which most callers are primarily interested in. Additionally it stores the remainder as a data member, which is easily accessible by the caller afterwards.

This way you can have many additional "return values", very useful if you implement database or networking calls, where lots of error messages may be needed but only in case an error occurs.

I entered this solution also in the C++ question that OP is referring to.

查看更多
初与友歌
4楼-- · 2018-12-31 05:18

Classes, Structures, Collections and Arrays can contain multiple values. Output and reference parameters can also be set in a function. Return multiple values is possible in dynamic and functional languages by means of tuples, but not in C#.

查看更多
与风俱净
5楼-- · 2018-12-31 05:19

Here are basic Two methods:

1) Use of 'out' as parameter You can use 'out' for both 4.0 and minor versions too.

Example of 'out':

using System;

namespace out_parameter
{
  class Program
   {
     //Accept two input parameter and returns two out value
     public static void rect(int len, int width, out int area, out int perimeter)
      {
        area = len * width;
        perimeter = 2 * (len + width);
      }
     static void Main(string[] args)
      {
        int area, perimeter;
        // passing two parameter and getting two returning value
        Program.rect(5, 4, out area, out perimeter);
        Console.WriteLine("Area of Rectangle is {0}\t",area);
        Console.WriteLine("Perimeter of Rectangle is {0}\t", perimeter);
        Console.ReadLine();
      }
   }
}

Output:

Area of Rectangle is 20

Perimeter of Rectangle is 18

*Note:*The out-keyword describes parameters whose actual variable locations are copied onto the stack of the called method, where those same locations can be rewritten. This means that the calling method will access the changed parameter.

2) Tuple<T>

Example of Tuple:

Returning Multiple DataType values using Tuple<T>

using System;

class Program
{
    static void Main()
    {
    // Create four-item tuple; use var implicit type.
    var tuple = new Tuple<string, string[], int, int[]>("perl",
        new string[] { "java", "c#" },
        1,
        new int[] { 2, 3 });
    // Pass tuple as argument.
    M(tuple);
    }

    static void M(Tuple<string, string[], int, int[]> tuple)
    {
    // Evaluate the tuple's items.
    Console.WriteLine(tuple.Item1);
    foreach (string value in tuple.Item2)
    {
        Console.WriteLine(value);
    }
    Console.WriteLine(tuple.Item3);
    foreach (int value in tuple.Item4)
    {
        Console.WriteLine(value);
    }
    }
}

Output

perl
java
c#
1
2
3

NOTE: Use of Tuple is valid from Framework 4.0 and above.Tuple type is a class. It will be allocated in a separate location on the managed heap in memory. Once you create the Tuple, you cannot change the values of its fields. This makes the Tuple more like a struct.

查看更多
梦该遗忘
6楼-- · 2018-12-31 05:19

A method taking a delegate can provide multiple values to the caller. This borrows from my answer here and uses a little bit from Hadas's accepted answer.

delegate void ValuesDelegate(int upVotes, int comments);
void GetMultipleValues(ValuesDelegate callback)
{
    callback(1, 2);
}

Callers provide a lambda (or a named function) and intellisense helps by copying the variable names from the delegate.

GetMultipleValues((upVotes, comments) =>
{
     Console.WriteLine($"This post has {upVotes} Up Votes and {comments} Comments.");
});
查看更多
妖精总统
7楼-- · 2018-12-31 05:21

You either return a class instance or use out parameters. Here's an example of out parameters:

void mymethod(out int param1, out int param2)
{
    param1 = 10;
    param2 = 20;
}

Call it like this:

int i, j;
mymethod(out i, out j);
// i will be 20 and j will be 10
查看更多
登录 后发表回答