我可以在方法中返回两个输出吗? [英] can I return two outputs in a method ?

查看:101
本文介绍了我可以在方法中返回两个输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要想要在方法中返回两个输出......是可能的???

I wanna want to return two outputs in a method ...is that possible ???

推荐答案

不是通过返回statement - 一个方法只能返回一个对象。



有两种方法可以做你想做的事:

1)返回一个包含你需要的两个(或更多)值的对象:

Not via a return statement - a method can only ever return a single object.

There are two ways to do what you want:
1) Return an object which contains the two (or more) values you need:
private KeyValuePair<string, int> MyMethod()
    {
    ...
    return new KeyValuePair<string, int>("hello", 666);
    }



2)添加 out ref 参数:


2) Add an out or ref parameter:

private string MyMethod(out int iValue)
    {
    ...
    iValue = 666;
    return "hello";
    }





[edit] int 部分KeyValuePair声明消失... - OriginalGriff [/ edit]



[edit]int parts of KeyValuePair declarations vanished... - OriginalGriff[/edit]


您好,



而不是 KeyValuePair ,你也可以使用元组

http://msdn.microsoft.com/en-us/library/dd268536.aspx [ ^ ]

Hi,

Instead of a KeyValuePair, you can also use a Tuple:
http://msdn.microsoft.com/en-us/library/dd268536.aspx[^]
private Tuple<string,int> MyMethod()
    {
    ...
    return new Tuple<string,int>("hello", 666);
    }




Tuple<string,int> x = MyMethod();
string str = x.Item1;
int i = x.Item2;



不仅具有2个元素的元组:有元组 s,包含1,2,3,4,5,6,7和8个元素: http://msdn.microsoft.com/en-us/library/system.tuple.aspx [ ^ ]



希望这会有所帮助。


There's not only a Tuple with 2 elements: there're Tuples with 1, 2, 3, 4, 5, 6, 7 and 8 elements: http://msdn.microsoft.com/en-us/library/system.tuple.aspx[^]

Hope this helps.


这篇关于我可以在方法中返回两个输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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