using System;
namespace ConsoleApplication13
{
class Program
{
static void outTest(out int x, out int y)
{//离开这个函数前,必须对x和y赋值,否则会报错。
//y = x;
//上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行
x = 1;
y = 2;
}
static void refTest(ref int x, ref int y)
{
x += 1;
y += x;
}
public static void Main()
{
//out test
int a, b;
//out使用前,变量可以不赋值
outTest(out a, out b);
Console.WriteLine("a={0};b={1}", a, b);
//无进有出
int c = 11, d = 22;
outTest(out c, out d);
Console.WriteLine("c={0};d={1}", c, d);
//ref test
//int m, n;
//refTest(ref m, ref n);
//上面这行会出错,ref使用前,变量必须赋值
//有进有出
int o = 11, p = 22;
refTest(ref o, ref p);
Console.WriteLine("o={0};p={1}", o, p);
}
}
}
C# out 与 ref 关键字 就是传递引用
原创
©著作权归作者所有:来自51CTO博客作者RandTsui的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:C# 代码运行时间统计
下一篇:C# 特性
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C# 索引器和ref、out关键字
这节讲三个小知识:索引器、ref、out。 索引器: 在一个类中,我们可以定义一
索引器 数组 赋值 数据 主方法 -
C#ref、out关键字详解
ref关键字是用来修饰参数的,如果一个形参用ref来修饰了,那么对应的实参也需要用ref、ref参数默认...
C# 关键字 ref out 赋值