我们如何从C#中的函数返回多个值?
在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
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短