元组
private (int number,int newNumber) CaculateByTuple(int number)
{
return (number: number,
newNumber: number * number);
}
private List<(int number, int newNumber)> CaculateByTuple(List<int> numbers)
{
return numbers.AsEnumerable().Select(
x =>
{
return (number: x,
newNumber: x * x);
})
.ToList();
}
匿名对象
private dynamic CaculateByDynamic(int number)
{
return new
{
number = number,
newNumber = number * number
};
}
ref&out
ref
private int CaculateByRef(ref int number)
{
//对于ref
//在进入方法CaculateByRef之前,number必须赋值
//在方法CaculateByRef里面,number可以不进行修改
number = 2;
return number * number;
}
out
private int CaculateByOut(out int number)
{
//对于ref
//在进入方法CaculateByOut之前,number不必赋值
//在方法CaculateByOut里面,number必须进行赋值
number = 3;
return number * number;
}
ref&out的坏处在于:
查询number值的引用时,只能查询到ref&out这个方法名上面,对于IDE来说,方法修改了一个变量值,而不是修改了number
在开发过程中的查询检索会遇到困难,如下所示:
测试方法
public void Test()
{
//ref
int number = 1;
int newNumber=CaculateByRef(ref number);
//out
newNumber = CaculateByOut(out number);
//元组
var returnValues = CaculateByTuple(number);
number = returnValues.number;
newNumber = returnValues.newNumber;
//匿名对象
var returnValues2 = CaculateByDynamic(number);
number = returnValues2.number;
newNumber = returnValues2.newNumber;
//元组2
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
var returnValues3 = CaculateByTuple(numbers);
}
示例
https://github.com/zLulus/NotePractice/blob/dev3/Console/ConsoleDemo/ReturnsMultipleValuesTest.cs