timeObject.GetTime(reftheHour,reftheMinute,reftheSecond)
如果在使用theHour、theMinute和theSecond这三个变量之前没有对它们进行初始化,就会产生一个编译错误:
Useofunassignedlocalvariable‘theHour‘
Useofunassignedlocalvariable‘theMinute‘
Useofunassignedlocalvariable‘theSecond‘
我们可以通过将这些变量初始化为0或其他对方法的返回值没有影响的值,以解决编译器的这个小问题:
inttheHour=0;
inttheMinute=0;
inttheSecond=0;
timeObject.GetTime(reftheHour,reftheMinute,reftheSecond)
这样就有些太麻烦了,这些变量传递给GetTime方法,然后被改变而已。为了解决这一问题,C#专门针对这一情况提供了out参数修饰符,它可以使一个参数无需初始化就可以被引用。例如,GetTime中的参数对它本身没有一点意义,它们只是为了表达该方法的输出。在方法中返回之前,Out参数中必须被指定一个值。下面是经过修改后的GetTime方法:
publicvoidGetTime(outinth,outintm,outints)
{
h=Hour;
m=Minute;
s=Second;
}
下面是新的GetTime方法的调用方法:
timeObject.GetTime(outtheHour,outtheMinute,outtheSecond);
本文章更多内容:<<上一页 - 1 - 2 - 3 - 4 |