C#是否支持多个返回值? [英] Does C# support multiple return values?

查看:208
本文介绍了C#是否支持多个返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的问题,如果我想做的事情很复杂/涉及到,那么我不希望您详细介绍...我读到这可能涉及结构或哈希或某些我还没有完成其他可怕的程序.如果是这样,我相信很快就会得到我的帮助.

This is a very basic question, and if what I am thinking of doing is complicated/involved, then I don't expect you to go into detail... I've read that this may involve structs or hash or some other scary procedure I've not gotten to yet. If so, I'm sure it'll get me soon.

研究类,方法和返回值.

Working on learning classes, methods, and return values.

我想让我的课程/方法返回当前时间"和分钟".确实很简单. 构造正确还是正确?

I'd like to have my class/method return Current Hour and Minute. Simple enough, really. Is this constructed correctly, or properly?

class MyClass
{
    public int GetHour (int hr, int min)
    {
        DateTime dt = DateTime.Now;
        int hour = dt.Hour;
        int minute = dt.Minute;

        return hour;
        return minute;

    }
}

然后,从Main()调用它:出现一些错误(No overload for methodUnreachable code detected)

And, calling it from Main(): Getting some errors (No overload for method and Unreachable code detected)

static void Main ( string[] args )
{
    MyClass mc = new MyClass ();
    Console.WriteLine ("Hour: {0} \n Minute: {1}", mc.GetHour());   
    Console.ReadLine ();
}

问题是:我要关闭吗?

推荐答案

如@ChaosPandion所述,在这种情况下,您将返回DateTime结构.

As mentioned by @ChaosPandion, in that specific case you would return a DateTime struct.

但是,一般来说,您将有以下选择:

In general, however, you would have the following options:

使用out参数

Using out parameters

这是一种通常可以正常工作的简单方法.但是,这有点笨拙,因为返回的结果通常是您希望传递函数参数的地方,并且方法签名可能变得冗长且难以重构.

This is a simple way that will usually always work. However, it is a bit clunky, as the result is returned where you usually would expect the function arguments to be passed and the method signature might get lengthy and hard to refactor.

public void GetTime(out int hr, out int min) 
{ 
    DateTime dt = DateTime.Now;
    hr = dt.Hour;
    min = dt.Minute;
}

static void Main(string[] args)
{
     // declare variables for out parameters first
     int hour, minute;
     GetTime(out hour, out minute);
}

使用数组

这是一个简单的方法,如果要返回的值具有相同的类型,则效果很好.

This is a simple method that works well if the values to be returned have the same type.

public int[] GetTime() 
{ 
    DateTime dt = DateTime.Now;
    return new[] { dt.Hour, dt.Minute};
}

使用属性包(属性包是仅包含属性的简单类)

Using a property bag (A property bag is a simple class which only has properties)

这非常方便,并且可以在以后不更改方法签名的情况下轻松修改返回值的类型和数量.

This is very convenient and allows easy modification of the type and number of returned values later on without changing the method signature.

class A
{ 
     int Prop1 { get; set; }
     int Prop2 { get; set; }
}

public A SomeMethod()
{
    return new A() { Prop1 = 1, Prop2 = 2 }
}    

使用元组

在C#4.0(要求VS 2010)中,您可以使用 Tuple<T1, T2, ...> 类:

In C# 4.0 (requires VS 2010) you can use the Tuple<T1, T2, ...> class:

public Tuple<int, int> GetTime() 
{ 
    DateTime dt = DateTime.Now;
    return Tuple.Create(dt.Hour, dt.Minute);
}

C#7.0元组

C#7.0添加了对多个返回值的支持.您可以编写如下代码以返回隐式创建的元组:

C# 7.0 adds support for multiple return values. You can write code like this to return an implicitly created tuple:

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

默认情况下,元组元素的名称为Item1Item2等,但是您也可以指定名称,例如

The tuple elements are names Item1, Item2, etc by default, but you can also specify names, e.g.

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

,然后通过这些名称访问元组元素:

and then access the tuple elements via those names:

var names = LookupName(id);
WriteLine($"found {names.first} {names.last}.");

这篇关于C#是否支持多个返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆