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 is used to pass an argument within a method. In this article, you'll learn the differences between these.
Ref
The REF keyword is used-to-pass an argument as a reference. This means if value of that parameter was changed in the method, it gets reflected in the calling method. An argument, that's passed using a ref keyword must be initialized in the calling method before it's passed to the called Method.
Out
The Out keyword are also used to pass a argument like ref keyword, but the argument can being passed without assigning any VA Lue to it. An argument, which is passed using a out keyword must being initialized in the called method before it returns back to calling Method.
Program with ref and out keyword
Public Static voidMain ()//Calling Method { intVal1 =0;//must be initialized intVal2;//OptionalExample1 (refval1); Console.WriteLine (VAL1); //val1=1Example2 ( outval2); Console.WriteLine (VAL2); //val2=2 } Static voidExample1 (ref intValue//called method{Value=1; } Static voidExample2 ( out intValue//called method{Value=2;//must be initialized } /*Output 1 2
Note
- Do is confused with the concept of passing by reference and the concept of reference type. These concepts is not the same.
- A value type or a reference type can be passed to method parameter by using ref keyword. There was no boxing of a value type when it was passed by reference.
- Properties cannot is passed to ref or out parameters since internally they is functions and not members/variables.
Ref and out in method overloading
Both ref and out cannot is used in method overloading simultaneously. However, ref and out is treated differently at Run-time but they is treated same at compile time (CLR doesn ' t differenti Ates between the IT created IL for ref and out). Hence methods cannot is overloaded when the one method takes a ref parameter and other method takes an out parameter. The following methods is identical in terms of compilation.
Public voidMethod ( out intA//Compiler Error "cannot define overloaded" { //method that differ only on ref and out "A =0;//must be initialized } Public voidMethod (ref inta) {//method that differ only on ref and out "}
However, method overloading can be do, if one method takes a ref or out argument and the other method takes simple AR Gument. The following example is perfectly valid to be overloaded.
class MyClass { publicvoid Method (int a) { } public void method (outint a) { // method differ in Signature. 0 // must be initialized } }
What
does you think?
I hope you'll enjoy the ref and out keywords while programming with C #. I would like to has feedback from my blog readers. Your valuable feedback, question, or comments about this article is always welcome.
difference between ref and out parameters