惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
GbyAI
GbyAI
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
I
InfoQ
V
Visual Studio Blog
罗磊的独立博客
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
Jina AI
Jina AI
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
The Cloudflare Blog
小众软件
小众软件
雷峰网
雷峰网
V
V2EX
人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog

博客园 - 流星啊。

vscode安装go所有插件 navicat15 for mysql激活 sql 循环语句几种方式 试用版SQL Server 2008 R2 提示评估期已过 常用SQL QQ web api Mysql数据库的分离和附加<转> Java字符串转换为日期和时间比较大小 Oracle中账户被锁定的解决方法 清除代码异味 EXT3.3在IE9上 , TreePanel click event 失效 在XP下配置自己的服务器IIS Extjs 树形设置选中 Window下删除SVN文件 GOF设计模式(转) ASCII表 博文阅读密码验证 - 博客园 查询最近10天要过生日的会员 Extjs中grid初始化时选中指定行的问题 - 流星啊。 - 博客园
C# 中ref out区别
流星啊。 · 2010-11-17 · via 博客园 - 流星啊。

1、使用ref型参数时,传入的参数必须先被初始化。对out而言,必须在方法中对其完成初始化。

2、使用ref和out时,在方法的参数和执行方法时,都要加Ref或Out关键字。以满足匹配。

3、out适合用在需要retrun多个返回值的地方,而ref则用在需要被调用的方法修改调用者的引用的时候。

在C#中,方法的参数传递有四种类型:传值(by value),传址(by reference),输出参数(by output),数组参数(by array)。

示例:

代码

static void Main(string[] args)
{
//outTest
int a, b; //没赋值
outTest(out a, out b);
Console.WriteLine(
"Out Test: a={0}, b={1}",a,b); //输出 a=10,b=20

int c=8,d=9; //赋值
outTest(out c,out d);
Console.WriteLine(
"Out Test: c={0}, d={1}", c, d);//输出 c=10,d=20//RefTest
int o, p;
// refTest(ref o, ref p); 错误,Ref 使用前变量必须赋值
o = 1; p = 2;
refTest(
ref o,ref p);
Console.WriteLine(
"Ref Test:o={0},p={1}", o, p); //输出 o=30,p=40

Console.ReadLine();
}
static void outTest(out int x,out int y)
{
//Console.WriteLine("befor out: x={0},y={1}",x,y); 报错,使用out后,x,y都被清空,需要重新赋值
x = 10;
y
= 20;
}
static void refTest(ref int x,ref int y)
{
Console.WriteLine(
"befor ref: x={0},y={1}", x, y); //输出 x=1,y=2
x = 30;
y
= 40;
}