C#中关键字ref与out的区别【转】
|
在C#中,ref与out是很特殊的两个关键字。使用它们,可以使参数按照引用来传递。 |
首先,我们来看一个简单的例子:
static void TestRefAndOut(){
string s1 = "Good Luck!";
TestRef(ref s1);
Console.WriteLine(s1);//output: Hello World!
}
static void TestRef(ref string str){
str = "Hello World!";
}
在TestRefAndOut()中将字符串s1以ref关键字的方式传到方法TestRef(ref string str)中,在这个方法中,我们改变了s1的引用变量str的值,最后,回到TestRefAndOut()方法后输出s1的值,发现其值已被改变。
将上例中的ref换成out,代码如下:
{
string s1 = "Good Luck!";
//TestRef(ref s1);
TestOut(out s1);
Console.WriteLine(s1);//output: Hello World!
}
static void TestOut(out string str)
{
str = "Hello World!";
}
同样,在将ref换成out后,会发现最后的输出仍然是相同的,那这两个关键字的区别是什么呢?
进一步测试:
ref:
{
string s1 = "Good Luck!";
TestRef(ref s1);
}
static void TestRef(ref string str)
{
Console.WriteLine(str);//output: Good Lick!
}
out
{
string s1 = "Good Luck!";
TestOut(out s1);
}
static void TestOut(out string str)
{
Console.WriteLine(str);//compile does not pass
}
ref的那段代码顺利编译,输出"Good Luck!",而out那段代码却无法通过编译,提示“Use of unassigned out parameter 'str' ”,即使用了未分配地址的out参数str。怎么回事呢?
原来out参数在进入方法的时候,C#会自动清空它的一切引用和指向,所以在上面的out例子中,必需先要为str参数赋值。如以下程序。
{
string s1 = "Good Luck!";
TestOut(out s1);
}
static void TestOut(out string str)
{
str = "Hello World!";
Console.WriteLine(str);//output: Hello World!
}
Ok,得到第一个区别: out 参数在进入方法(函数)时后清空自己,使自己变成一个干净的参数,也因为这个原因必须在方法返回之前或再使用out参数前为 out 参数赋值(只有地址没有值的参数是不能被.net接受的);而ref参数是不需要在被调用方法使用前先赋值的,甚至也可以被调用方法中不改变ref参数的值,这都不会引起编译错误。
在继续看一段代码:
ref:
{
string s1;
TestRef(ref s1);
Console.WriteLine(s1);//compile does not pass!
}
static void TestRef(ref string str)
{
str = Hello World!";
}
out:
static void TestRefAndOut()
{
string s1;
TestOut(out s1);
Console.WriteLine(s1);//output: Hello World!
}
static void TestOut(out string str)
{
str = "Hello World!";
}
这回发现,ref这段代码无法编译了,s1是一个空引用,所以无法使用。而out参数则因为上述的那个原因,它不在乎s1是不是空引用,因为就算s1不是空引用,它也会把s1变成空引用的。Ok,第二个区别:ref参数在使用前必需初始化,而out不需要。嗯,由上边两个区别可以引申一下,out参数只进不出,ref参数有进有出。在用法上概括一下就是:out适合用在需要retrun多个返回值的地方,而ref则用在需要被调用的方法修改调用者的引用的时候。
C#中关键字ref与out的区别【转】的更多相关文章
- 简述C#中关键字var和dynamic的区别
C#中关键字var和dynamic的区别如下: 1.var申明的变量必须初始化,dynamic申明的变量无需初始化. 2.var关键字只能在方法内部申明局部变量,dynamic关键字可用于局部变量,字 ...
- C#中的ref和out的区别
转载原地址 http://www.cnblogs.com/gjahead/archive/2008/02/28/1084871.html ref和out的区别在C# 中,既可以通过值也可以通过引用传递 ...
- Spring框架xml配置中属性ref与value的区别
1.spring批量扫描mybatis的mapper,使用value 2.spring管理mybatis的单个mapper,用的是ref 虽然引用的是同一个bean,但两个对象的属相类型明显不一样,一 ...
- Python中关键字global与nonlocal的区别
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xCyansun/article/details/79672634终于下定决心学习Python了.既然 ...
- Java中关键字super与this的区别
一.super关键字 在JAVA类中使用super来引用父类的成分,用this来引用当前对象,如果一个类从另外一个类继承,我们new这个子类的实例对象的时候,这个子类对象里面会有一个父类对象.怎么去引 ...
- 转载 Python中关键字global与nonlocal的区别
转载自CSDN 雁丘1990, 原文地址: https://blog.csdn.net/xcyansun/article/details/79672634 这篇文章写的很赞, 条理清晰, 分析循序渐进 ...
- C#中关键字ref修饰类对象或结构体[转]
using System; using System.Collections.Generic; using System.Text; namespace CSharpTest { struct Dog ...
- Java中关键字throw和throws的区别
==========================================题外话===================================================== 今 ...
- .net 中 ref out params的区别
C#中有三个关键字-ref,out ,params,虽然本人不喜欢这三个关键字,因为它们疑似破坏面向对象特性.但是既然m$把融入在c#体系中,那么我们就来认识一下参数修饰符ref,out ,param ...
随机推荐
- hdu 5625 Clarke and chemistry
Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke turned i ...
- IOS7 UITableView一行滑动删除后 被删除行的下一行的点击事件将被忽略解决办法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSI ...
- Word Amalgamation
Problem Description In millions of newspapers across the United States there is a word game called J ...
- Davinci开发板DM368 nandwrite.c简要分析
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <limits.h ...
- mapkit定位以及俯视视图
1.导入框架MapKit.framework,CoreGraphics.framework
- iOS FMDB
FMDB FMDB概述 什么是FMDB * FMDB是iOS平台的SQLite数据库框架 * FMDB以OC的方式封装了SQLite的C语言API FMDB的优点 * 使用起来更加面向对象,省去了很多 ...
- 手把手教你使用Git(转)
Git使用教程 2014-10-25 14:29 by 云溪0707, 10532 阅读, ... 评论, 收藏, 编辑 Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系 ...
- stack 集合栈计算机 (摘)
有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始为空的栈,并且支持以下操作:PUSH:空集“{}”入栈DUP:把当前栈顶元素复制一份后再入栈UNION:出栈两个集合,然后把两者的并集入 ...
- 基于Cookie跨域的单点登录问题
由于项目中,需要用的单点登录,主要的思路是:系统1:用户名密码-->写入Cookie-->其他系统读取Cookie. 1.在同一个服务器下的Cookie共享 @Component(&quo ...
- [转]在ITunes播放中前进、后退五秒的快捷键
在ITunes播放中前进.后退五秒的快捷键 如题,快捷键为Command+Alt+方向键.听Podcast一两句没听清楚的时候很有用. reference: http://mac.pcbeta.com ...