|
 |
推荐文章 |
|
|
|
|
|
|
|
|
|
|
| 作者:efoxxx 日期:2005-8-8 9:43:41 |
|
|
声明:我在翻译的时候,加了一些自己的看法以及其它的一些比较,希望能让大家看的更明白,理解更透彻。
有什么建议请mail:efoxxx@263.net
C#中的out和ref参数
out和ref参数是用于让传递到方法的参数中带回返回值。
在你需要让方法返回多于一个返回值的情况下,这俩个参数很有用。
◆out参数
out参数可以用于让同一个变量参数既传参又带回返回值。(有点像C中的指针哟)
public class mathClass
{
public static int TestOut(out int iVal1, out int iVal2)
{
iVal1 = 10;
iVal2 = 20;
return 0;
}
public static void Main()
{
int i, j; //变量i,j不需要初始化
Console.WriteLine(TestOut(out i, out j));
Console.WriteLine(i);
Console.WriteLine(j);
}
}
efoxxx补充:
这里我想补充一个谭浩强老师的一个经典的C程序例子:swap
public class swapClass
{
public static int SwapOut(out int iVal1, out int iVal2)
{
int temp;
temp = iVal1;
iVal1 = iVal2;
iVal2 = temp;
return 0;
}
public static void Main()
{
int i, j; // 变量i,j不需要初始化
SwapOut(out i, out j);
Console.WriteLine(i);
Console.WriteLine(j);
}
}
◆ref参数(引用)
与Java和C++的概念是一样的。
你也可以用ref来得到多于一个的返回参数。
namespace TestRefP
{
using System;
public class myClass
{
public static void RefTest(ref int iVal1 )
{
iVal1 += 2;
}
public static void Main()
{
int i; // variable need not be initialized
i = 3;
RefTest(ref i );
Console.WriteLine(i);
}
}
} |
|
|
|
|
|
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。 |
|
|
|
|
|
责任编辑: 原点 |
投稿作者: efoxxx |
|
|
信息来源: 网络 |
录入时间: 2005-8-8 9:43:41 |
|
|
|
| |
|