本文介绍了如何从函数中检索多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

(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;
}

推荐答案

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.



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


这篇关于如何从函数中检索多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 12:23