简体   繁体   中英

How to get multiple value as return type from a method in C#?

Suppose in a C# class there is a method Like _Abc(int a, int b) usually it takes 2 parameters.

Is it possible to get 4 int values from _Abc(int a, int b) method as return?

You could return a Tuple<int, int, int, int> . Returning tuples as part of a public API isn't very informative however, so in that case I would create a class to hold the return values so you can name the values.

Though the answers so far (return a tuple, return a collection, return a custom class/struct, or use out parameters) are reasonable answers, a better answer is: don't do that in the first place . A method that returns four things is probably a badly-designed method.

Can you redesign the type so that you have, say, four different methods (or properties) each of which returns one thing? There may be a better pattern for you to use. Can you explain your scenario in more detail?

You can return a custom type which is the most flexible and extensible approach.

public static Foo GetFoo(int a, int b)
{
    Foo foo = new Foo();
    foo.A = 1;
    foo.B = 2;
    foo.C = 3;
    foo.D = 4;
    return foo;
}

Here's the pseudo class

public class Foo
{
    public int A{ get; set; }
    public int B{ get; set; }
    public int C{ get; set; }
    public int D{ get; set; }
}

Another way (with .NET 4) is using a Tuple

public static Tuple<int,int,int,int> GetFoo(int a, int b)
{
    return Tuple.Create(1,2,3,4);
}

Which is not so readable as the class approach since you read the properties in this way:

var values = GetFoo(1, 2);
int A = values.Item1;
int B = values.Item2;
int C = values.Item3;
int D = values.Item4;

So a tuple is somehwhat anonymous.

If you just want to return one or two additional parameters you can also use an out parameter as in the TryParse methods.

DateTime dt;
String str = "01.01.2013";
if(DateTime.TryParse(str, out dt))
{
    // so this method returns two variables:
    // a bool as return value and a DateTime as out parameter
    Console.WriteLine("Year: " + dt.Year);
}

除了Lee的回答,你可以添加一些refout参数,即:

_Abc(int a, int b, out int c, out int d, out int e, out int f)

You could also create a function like this with out parameters.

public void _Abc(int a, int b, out res1, out res2, out res3, out res4)
   {
    res1 = 1;
    res2 = 2;
    res3 = 3;
    res4 = 4;
   }

If you can change the method you can use out parameters. http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.80).aspx

_Abc(int a, int b, out int c, out int d, out int e, out int f){
c=1;
d=2;
e=3;
f=4;
}

您可以将它们作为输出参数返回

If you're returning 4 completely different things, then it would be best to create a new type. For example, if you're returning the height, width, top, and bottom values of a newly created shape, then you should create a class that holds all of those values with the proper variable names.

If you're holding 4 of the same type of thing then it may make sense to put them all into a collection of some sort. For example, if the four numbers represent the area of four different shapes then it would make sense to return a List<int> or an int[] .

I think you need easy ans, right?

Try to understand this simple code (just copy and paste):

class Class1
{
    private List<int> _Abc(int a, int b)
    {
        // do job

        int x = 128, y = 256, z = 512;
        List<int> returns = new List<int>();
        returns.Add(x);
        returns.Add(y);
        returns.Add(z);

        return returns;
    }

    public void anotherMethod()
    {
        List<int> simple = new List<int>();
        simple = _Abc(55, 56);

        int[] _simple = simple.ToArray();
        for (int i = 0; i < simple.Count; i++)
        {
            Console.WriteLine("" + _simple[i]);
        }
        //Console.ReadKey();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM