int GetRest(out int all, out int usedCount,out int invalidCount)
{
all = 10; // all = GetAll();
usedCount = 5; // usedCount = GetUsedCount();
invalidCount = 3; // invalidCount = GetinvalidCount();
return all - usedCount - invalidCount;
}
{
all = 10; // all = GetAll();
usedCount = 5; // usedCount = GetUsedCount();
invalidCount = 3; // invalidCount = GetinvalidCount();
return all - usedCount - invalidCount;
}
用的时候发现很多时候不需要得到三个输出参数,于是写了个无参数的重载:
int GetRest()
{
int temp;
return GetRest(out temp, out temp, out temp);
}
{
int temp;
return GetRest(out temp, out temp, out temp);
}
F6,编译通过!
吼吼,可是,输出数据非常奇怪!于是单步调试,F10,局部变量窗口发现all,usedCount,invalidCount居然是同样的值!当然也就马上明白是怎么回事:out传参是引用传递!三个变量指向同一个对象,对值类型这种情况还是很少见,不借助out/ref参数,想实现多个值类型变量指向同一个对象也难呐。
于是最终代码:
int GetRest()
{
int temp1,temp2,temp3;
return GetRest(out temp1, out temp2, out temp3);
}
{
int temp1,temp2,temp3;
return GetRest(out temp1, out temp2, out temp3);
}