Difference between ref and out parameters
Original link: http://www.dotnet-tricks.com/Tutorial/csharp/K0Hb060414-Difference-between-ref-and-out-parameters.html
Following content is directly reprinted from above link, please go to the original link for more details.
Difference between ref and out parameters
Author: Shailendra Chauhan
Ref and out parameters are used to pass an argument within a method. In this article, you will learn the differences between these two parameters.
Ref
The ref keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.
Out
The out keyword is also used to pass an argument like ref keyword, but the argument can be passed without assigning any value to it. An argument that is passed using an out keyword must be initialized in the called method before it returns back to calling method.
Program with ref and out keyword
public static void Main() //calling method
{
int val1 = ; //must be initialized
int val2; //optional Example1(ref val1);
Console.WriteLine(val1); // val1=1 Example2(out val2);
Console.WriteLine(val2); // val2=2
} static void Example1(ref int value) //called method
{
value = ;
}
static void Example2(out int value) //called method
{
value = ; //must be initialized
}
/* Output
1
2
Note
- Do not be confused with the concept of passing by reference and the concept of reference type. These two concepts are not the same.
- A value type or a reference type can be passed to method parameter by using ref keyword. There is no boxing of a value type when it is passed by reference.
- Properties cannot be passed to ref or out parameters since internally they are functions and not members/variables.
Ref and out in method overloading
Both ref and out cannot be used in method overloading simultaneously. However, ref and out are treated differently at run-time but they are treated same at compile time (CLR doesn't differentiates between the two while it created IL for ref and out). Hence methods cannot be overloaded when one method takes a ref parameter and other method takes an out parameter. The following two methods are identical in terms of compilation.
public void Method(out int a) // compiler error “cannot define overloaded”
{
// method that differ only on ref and out"
a = ; //must be initialized
}
public void Method(ref int a)
{
// method that differ only on ref and out"
}
However, method overloading can be done, if one method takes a ref or out argument and the other method takes simple argument. The following example is perfectly valid to be overloaded.
class MyClass
{
public void Method(int a)
{ }
public void Method(out int a)
{
// method differ in signature.
a = ; //must be initialized
}
}
What do you think?
I hope you will enjoy the ref and out keywords while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Difference between ref and out parameters的更多相关文章
- C# Best Practices - Specify Clear Method Parameters
Improve parameters parameter order public OperationResult PlaceOrder(Product product, int quantity, ...
- Async/Await FAQ
From time to time, I receive questions from developers which highlight either a need for more inform ...
- Threading in C#
http://www.albahari.com/threading/ PART 1: GETTING STARTED Introduction and Concepts C# supports par ...
- 编程概念--使用async和await的异步编程
Asynchronous Programming with Async and Await You can avoid performance bottlenecks and enhance the ...
- IronPython .NET Integration官方文档翻译笔记
http://ironpython.net/documentation/dotnet/这是原文地址 以下笔记仅记录阅读过程中我认为有必要记录的内容,大多数都是依赖翻译软件的机翻,配合个人对代码的理解写 ...
- 不完全翻译:Threading in C#-Getting Started
Introduction(引入,介绍) and Concepts(概念) 原文地址:http://www.albahari.com/threading/ 注:水平有限不能全文翻译,备注了个别字段和短句 ...
- 关于C#你应该知道的2000件事
原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...
- SAP 出库单新版
*&---------------------------------------------------------------------* *& Report ZSDR045 ...
- ABAP 出库单打印 产品 A搭A A搭B显示方式
*&---------------------------------------------------------------------* *& Report *& ...
随机推荐
- 设备文件的创建mknod
设备文件是通过mknod命令来创建的.其命令格式为: mknod [OPTION]... NAME TYPE [MAJOR MINOR] TYPE取值: 主设备号和次设备号两个参数合并成一个16位的无 ...
- APP快速通过苹果AppStore审核九大诀窍
[IT168技术]对于移动应用开发者来说, 最令人沮丧的可能莫过于辛辛苦苦开发的应用, 没能通过苹果AppStore的审核,或者在应用更新时遭遇下架.苹果的AppStore的审核流程和标准, 一向不透 ...
- 如何在Fedora或CentOS上使用Samba共享
如今,无论在家里或者是办公场所,不同的电脑之间共享文件夹已不是什么新鲜事了.在这种趋势下,现代操作系统通过网络文件系统的方式使得电脑间数据的交换变得简单而透明.如果您工作的环境中既有微软的Window ...
- Mac上安装Tomcat服务器
1.下载Tomcat安装包,下载地址:http://tomcat.apache.org/ 选择你想要的版本,在这里我选择下载Tomcat 8 ps:你也可以使用axel多线程下载命令下载 下载的速度很 ...
- FMDB 二次封装工具类,让你快速学会封装,集成数据库
来源:StrivEver 链接:http://www.jianshu.com/p/4c77aee0b41c 上个版本为了增加用户体验,部分页面集成了离线缓存数据功能,于是就在项目里使用了数据库管理离线 ...
- IOS webView快照
这个功能就是对网页的存储,存储成png格式的图片 且不失真 很棒的一个小方法.具体实现如下: - (void)webViewDidFinishLoad:(UIWebView *)webView1 { ...
- 2dx解析cocosbuilder中使用layer时的缺陷
2dx解析cocosbuilder中使用layer时的缺陷 cocos2d-x 3.7 cocosbuilder中的layer通常会用到触摸属性: 但是在2dx解析布局文件的时候,却很多属性都没解析: ...
- ArcGIS 投影转换(AE)
private void btnOK_Click(object sender, EventArgs e) { try { CheckError(); this.checkEdit1.Enabled = ...
- 《Cortex-M0权威指南》之体系结构---程序映像和启动流程
转载请注明来源:cuixiaolei的技术博客 我们先来看看程序映像. 通常,Cortex-M0处理器的程序映像时从地址0x00000000处开始的. 程序映像开始处时向量表,其中包含了异常的其实地址 ...
- Linux_内存查询
查询磁盘使用情况: [root@css-management ~]# df -hl 文件系统 容量 已用 可用 已用% 挂载点 Filesystem Size Used Avail Use% Moun ...