简体   繁体   English

一个方法可以返回多个值吗?

[英]Can a method return more than one value?

So I was wondering if a method can return more than one value? 所以我想知道一种方法是否可以返回多个值? I am trying to write a program which returns the lowest and greatest value of an array: 我正在尝试编写一个程序,该程序返回数组的最低和最高值:

    public int GetMinMaxValue(int[] numbers)
    {
        int lowest;
        int greatest;

        foreach (int item in numbers)
        {
            // some code assigning the values
        }

        return lowest; // and is it possible to return greatest too?
    }

Thanks! 谢谢!

Answer is simply No , you can not send more than one parameter using return statement. 答案很简单: ,您不能使用return语句发送多个参数。

but logically YES by using parameter modifier out . 但是在逻辑上通过使用参数修饰符out

The out keyword causes arguments to be passed by reference. out关键字使参数通过引用传递。 To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. 要使用out参数,方法定义和调用方法都必须显式使用out关键字。

From MSDN : MSDN

Although variables passed as out arguments do not have to be initialized before being passed, the called method is required to assign a value before the method returns. 尽管作为传出参数传递的变量不必在传递之前进行初始化,但是在方法返回之前,被调用方法需要分配一个值。

So before leaving the function you should assign the values for both lowest and greatest parameters and if you don't intialize them ofcourse compiler warn you. 因此,在离开函数之前,应同时为lowestgreatest参数分配值,如果不初始化它们,当然编译器会警告您。

Try This: 尝试这个:

public int GetMinMaxValue(int[] numbers,out int lowest,out int greatest)
{
    lowest=0; //not required if there is a definite assignment happens in your code
    greatest=0;//not required if there is a definite assignment happens in your code
    foreach (int item in numbers)
    {
        // some code assigning the values
    }

}

You can call above method as below: 您可以按以下方式调用上述方法:

int lowest; //not assigned to any value
int greatest;//not assigned to any value
GetMinMaxValue(numbers,out lowest,out greatest)

once after calling the above method your lowest and greatest variables will have the values assigned by GetMinMaxValue() method. 调用上述方法后, lowestgreatest变量将具有由GetMinMaxValue()方法分配的值。

Yes, there is a number of ways to do it: 是的,有很多方法可以做到:

  • Return an array, 返回一个数组,
  • Return a Tuple<T1,T2> 返回一个Tuple<T1,T2>
  • Return a custom class that you define for the purpose of returning several things from a method 返回您定义的自定义类,以从方法中返回一些内容
  • Use out or ref parameters. 使用outref参数。

In the first three cases you return a single object, but you stuff multiple values in it. 在前三种情况下,您返回一个对象,但是在其中填充了多个值。 In the last case you do not return a value at all, relying on an alternative way of passing the data back to the caller. 在最后一种情况下,您根本不返回任何值,而是依赖于将数据传递回调用方的另一种方法。

No. 没有。

But , return type doesn't have to be a primitive number. 但是 ,返回类型不必是原始数字。 You can return a List/Array of objects, Or you can return a object which has several properties inside of it. 您可以返回对象的列表/数组,也可以返回其中具有多个属性的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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