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 *& ...
随机推荐
- IPC——信号
Linux进程间通信——使用信号 一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过 ...
- 关于OpenGL+GLSL深度贴图采样
作者:Nin+.Lee 邮箱:lilei9110@gmail.com * 本文属原创,转载请注明出处. 在GLSL中,存在着sampler2D和sampler2DShadow两种2D贴图采样器.在对一 ...
- iOS开发,让数据更安全的几个加密方式
任何应用的开发中安全都是重中之重,在信息交互异常活跃的现在,信息加密技术显得尤为重要.在app应用开发中,我们需要对应用中的多项数据进行加密处理,从而来保证应用上线后的安全性,给用户一个安全保障.这篇 ...
- 集成支付宝钱包支付ios SDK的方法和经验
没想到,支付宝的SDK是我目前用过的所有第三方SDK中最难用的一个了. 下载 首先,你要想找到这个SDK,都得费点功夫.现在的SDK改名叫移动支付集成开发包了,下载页面在 这里 的 “请点此下载集成开 ...
- C# 之 读写文件
1.使用 FileStream 读写文件 添加命名空间引用: using System; using System.Collections.Generic; using System.Text; us ...
- MyBatis 环境搭建
1.为什么我们学习框架? 提高开发效率,框架是别人写好的工具类,我们需要遵循其规则进行操作 2.我们学习哪些框架 A.持久层框架:MyBatis 什么是持久化? 狭义:把数据永久性的保存到数据当中 广 ...
- 程序编码(机器级代码+汇编代码+C代码+反汇编)
[-1]相关声明 本文总结于csapp: 了解详情,或有兴趣,建议看原版书籍: [0]程序编码 GCC调用了一系列程序,将源代码转化成可执行代码的流程如下: (1)C预处理器扩展源代码,插入所有用#i ...
- jQuery中的渐变动画效果
jQuery中的渐变动画效果jQuery中的渐变动画效果
- selenium遍历控件集合
场景:需要重复增加地址栏信息,如果地址信息超过了5个就不开始增加 如图: 1.找到控件集合,在遍历每个子元素,在进行选择 1.先找到最外层的div的控件集合 2.外层的css定位为: int star ...
- Oracle数据库作业-6 29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 select tname,prof from teacher where depart = '计算机系' and prof not in ( select prof from teacher where depart 。
29.查询选修编号为"3-105"课程且成绩至少高于选修编号为"3-245"的同学的Cno.Sno和Degree,并按Degree从高到低次序排序. selec ...