如何从函数中检索多个值? [英] How to retrieve more than values from a function?
本文介绍了如何从函数中检索多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从一个函数中检索多个值?
(string1,string2)= sourceValues(sourceId);
公共字符串sourceValues(字符串sourceId)
{
#做点事
返回string1,string2;
}
How to retrieve more than one values from a function?
(string1,string2) = sourceValues(sourceId);
public string sourceValues(string sourceId)
{
#do something
return string1, string2;
}
推荐答案
您可以执行以下操作:
You can do this:
Tuple<string,string> strTuple = sourceValues(sourceId);
// now strTuple.Item1 is the first string, and strTuple.Item2 is the second string
public Tuple<string,string> sourceValues(string sourceId)
{
// do something
return new Tuple<string,string>(string1,string2);
}
希望对您有所帮助.
Hope this helps.
我们应该从函数中返回Collection.
例如,我正在搜索从给定来源到目的地的巴士列表.
公共列表< string> GetBusInformation(字符串strSource,字符串strDestination)
{
List< string> objBusInformation = new List< string>();
//在此处写出找到总线的逻辑,找到总线后应将其添加到List< string>
//返回List< string>对象.
返回objBusInformation
}
问候,
RK
Hi,
We should return the Collection from the functions.
Say for example, I am searching for list of Buses that are travelling from given source to destination.
public List<string> GetBusInformation(string strSource, string strDestination)
{
List<string> objBusInformation=new List<string>();
// Here Write the Logic for finding the buses when the Bus is found it should be added to List<string>
// return the List<string> object.
return objBusInformation
}
Regards,
RK
以下是一些示例,
Here is some samples,
using System;
using System.Collections.Generic;
class Program
{
static void GetTwoNumbers(out int number1, out int number2)
{
number1 = (int)Math.Pow(2, 2);
number2 = (int)Math.Pow(3, 2);
}
static KeyValuePair<int, int> GetTwoNumbers()
{
return new KeyValuePair<int, int>((int)Math.Pow(2, 2), (int)Math.Pow(3, 2));
}
static void Main()
{
// Use out parameters for multiple return values.
int value1;
int value2;
GetTwoNumbers(out value1, out value2);
Console.WriteLine(value1);
Console.WriteLine(value2);
// Use struct for multiple return values.
var pair = GetTwoNumbers();
Console.WriteLine(pair.Key);
Console.WriteLine(pair.Value);
}
}
问候和感谢
sarva
regards and thanks
sarva
这篇关于如何从函数中检索多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文