在c#中可以使用以下方法返回多个值-
引用参数
输出参数
返回数组
返回元组
class Program{ static int ReturnMultipleValuesUsingRef(int firstNumber, ref int secondNumber){ secondNumber = 20; return firstNumber; } static void Main(){ int a = 10; int refValue = 0; var res = ReturnMultipleValuesUsingRef(a, ref refValue); System.Console.WriteLine($" Ref Value {refValue}"); System.Console.WriteLine($" Function Return Value {res}"); Console.ReadLine(); } }
Ref Value 20 Function Return Value 10
class Program{ static int ReturnMultipleValuesUsingOut(int firstNumber, out int secondNumber){ secondNumber = 20; return firstNumber; } static void Main(){ int a = 10; int outValue = 0; var res = ReturnMultipleValuesUsingOut(a, out outValue); System.Console.WriteLine($" Out Value {outValue}"); System.Console.WriteLine($" function Return Value {res}"); Console.ReadLine(); } }
Out Value 20 Function Return Value 10
class Program{ static int[] ReturnArrays(){ int[] arrays = new int[2] { 1, 2 }; return arrays; } static void Main(){ var res = ReturnArrays(); System.Console.WriteLine($"{res[0]} {res[1]}"); Console.ReadLine(); } }
1 2
class Program{ static Tuple<int, int>ReturnMulitipleVauesUsingTuples(){ return new Tuple<int, int>(10, 20); } static void Main(){ var res = ReturnMulitipleVauesUsingTuples(); System.Console.WriteLine($"{res.Item1} {res.Item2}"); Console.ReadLine(); } }
10 20