是否可以返回多个值,以及如何检索呢? [英] Is it possible to return more than 1 values and how to retrieve it?

查看:53
本文介绍了是否可以返回多个值,以及如何检索呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,并尝试对其进行实验.

This is my code and tried to experiment on it.

public string _getContact(int contactID)
{

//i had also do something in my contactID

string val1="";
string val2="";

//something to do in this part

return val1;
return val2;

}



我怎么可能找回它呢?



And how do i possibly retrieve it?

//dont know what to do this part
string pasval1 = _getContact();
string pasval2 = _getContact();

推荐答案

您可以使用以下参数: http://msdn.microsoft.com/en-us/library/t3c3bfhx( v = vs.71).aspx [ ^ ]

或者更好:返回一个对象
You can either use out parameters : http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.71).aspx[^]

Or better: Return an object
public class Contact{
   public string Firstname {get;set;}
   public string Lastname {get;set;}
}


....

public Contact _getContact(int contactID)
{
   Contact c = new Contact();
   c.Firstname = "Bill";
   c.Lastname = "Gates";
   return c;
}




如显示的那样,您不能使用多个return语句.当执行return语句时,控件将返回上一个线程.请检查下面的链接以获取更多信息.

http://csharp.net-tutorials.com/basics/functions/ [
Hi,

You can''t use multiple return statements as you have shown. When the return statement gets executed the control goes back to the previous thread. Please check below link for more clarity.

http://csharp.net-tutorials.com/basics/functions/[^]

In this particular case you can use an array or a comma separated string to combine multiple values in return.

Hope this helps
Sebastian


你好,

在这种情况下,您可以传递List< string>
并返回列表.这两个值都会使您进入列表的索引

hello,

In this case you can pass the List<string>
and return the list. both value will you get on index of list

List<String> list = new List<String>();

list.add("aa");
list.add("bb");

return list;


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

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