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 *& ...
随机推荐
- NULL、NUL、‘\0’、0以及EOF
0 is an integer constant, '\0' is a character constant, nul is the name of the character constant. N ...
- mkinitrd 与 mkinitramfs
转载:http://blog.csdn.net/mayouyang/article/details/3997849 在进行内核编译时,需要进行制作initrd.img.在Fedora下面一般是用mki ...
- PathAnimation
使用Blend制作PathAnimation 1:选中Path转换为运动路径 2:选择目标对象 PathAnimation使用动态的Path PathAnimation动画在播放的时候,PahtGeo ...
- OC 实现的几个排序算法
和在VC++6.0里相比 在OC里面实现 不算困难 可是我用惯了C/C++呢 快速排序,冒泡排序,直接插入排序和折半插入排序,希尔排序,堆排序,直接选择排序 /******************** ...
- GCD三种队列
:dispatch_get_global_queue 后台执行队列 :dispatch_get_main_queue 主队列 :dispatch_queue_create("test&quo ...
- 【Linux/Ubuntu学习3】解决ubuntu解压windows生成的zip文件时乱码问题
在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 虽然2005年就有人把这报告为bu ...
- 《算法导论》习题解答 Chapter 22.1-6(求universal sink 通用汇点)
思路:设置两个游标i指向行,j指向列,如果arr[i][j]==1,则i=max{i+1,j},j++:如果arr[i][j]==0,则j=max{i+1,j+1}. 伪代码: has_univers ...
- Shell学习笔记 - 环境变量配置文件
一.source命令 功能:在当前bash环境下读取并执行配置文件中的命令 1. 命令格式 source 配置文件 或 . 配置文件 2. 命令示例 [root@localhost ~]# sou ...
- 剑指Offer03 逆序输出链表&链表逆序
多写了个逆序链表 /************************************************************************* > File Name: ...
- codeforces 434B B. Nanami's Digital Board(分治)
题目链接: B. Nanami's Digital Board time limit per test 1 second memory limit per test 256 megabytes inp ...