ref与out的区别
来源:互联网 发布:qq群排名优化软件 编辑:程序博客网 时间:2024/05/29 11:43
总结以上四条得到ref和out使用时的区别是:
①:ref指定的参数在函数调用时候必须初始化,不能为空的引用。而out指定的参数在函数调用时候可以不初始化;
②:out指定的参数在进入函数时会清空自己,必须在函数内部赋初值。而ref指定的参数不需要。
代码:
class Program
{
static void Main(string[] args)
{
Program pg = new Program();
int x = 10;
int y = 20;
pg.GetValue(ref x, ref y);
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadLine();
}
public void GetValue(ref int x, ref int y)
{
x = 521;
y = 520;
}
}
{
static void Main(string[] args)
{
Program pg = new Program();
int x = 10;
int y = 20;
pg.GetValue(ref x, ref y);
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadLine();
}
public void GetValue(ref int x, ref int y)
{
x = 521;
y = 520;
}
}
class Program
{
static void Main(string[] args)
{
Program pg = new Program();
int x=10;
int y=233;
pg.Swap(out x, out y);
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadLine();
}
public void Swap(out int a,out int b)
{
int temp = a; //a,b在函数内部没有赋初值,则出现错误。
a = b;
b = temp;
}
}
{
static void Main(string[] args)
{
Program pg = new Program();
int x=10;
int y=233;
pg.Swap(out x, out y);
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadLine();
}
public void Swap(out int a,out int b)
{
int temp = a; //a,b在函数内部没有赋初值,则出现错误。
a = b;
b = temp;
}
}
0 0
- ref与out的区别
- Ref与Out的区别
- out与ref的区别
- ref与out的区别
- ref与out的区别
- ref与out的区别
- out 与 ref 的区别
- Ref与Out的区别
- ref与out的区别
- ref与out的区别
- ref 与out的区别
- Ref 与 Out 的区别
- ref与out区别
- out与ref区别
- ref与out区别
- ref和out的使用与区别
- ref与out之间的区别整理
- C#中ref与out的区别
- 【JZOJ4861】【NOIP2016提高A组集训第7场11.4】推冰块
- DAY20:leetcode #45 Jump Game II
- 自由与社会
- 自定义柱状图
- ubuntu报错permission
- ref与out的区别
- poj 1014 Dividing(多重背包/母函数)
- jmeter sampler中各个元件的执行顺序
- css布局之三列布局
- 一天一条Linux指令-uptime
- javascript代驾问题(this指向)
- Java虚拟机学习 - 体系结构 内存模型
- 打鼹鼠
- 将轻松的留给生活,沉重的留给写作——读《灯下尘》(七堇年)