Difference between ref and out parameters
2015-10-13 16:00
405 查看
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
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.
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.
[b]What do you think?[/b]
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.
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 = 0; //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 = 1; } static void Example2(out int value) //called method { value = 2; //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 = 0; //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 = 0; //must be initialized } }
[b]What do you think?[/b]
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.
相关文章推荐
- js日期格式化(javascript Date format)
- json字符串转换
- jQuery-autoComplete实例
- jQuery-autoComplete实例
- jQuery-autoComplete实例
- String 转json方法
- 学习小笔记---nodejs
- jquery 评分插件 juqer raty
- jquery easyui 输入框 禁止输入负数 设置属性data-options="min:0,required:true"
- jquery attr()和css()的区别
- nodejs npm install全局安装和本地安装的区别
- jquery.validate验证表单,自己的js提交数据,返回json
- 如何检查百度统计安装是否成功---js引用版
- 利用FullPage.js实现全屏滚动插件
- [CSS]图片水平排列并且有固定间隔
- js为什么运行完后会出现undefined
- Google Protocol Buffer 的使用和原理
- Javascript中的循环变量声明,到底应该放在哪儿?
- [LeetCode][JavaScript]Nim Game
- 手机与电脑搜索存在差异的原因总结网址:52xianfeng.com