且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从函数中检索多个值?

更新时间:2023-02-11 21:14:09

您可以执行以下操作:
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