difference between ref and out parameters

Source: Internet
Author: User

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
    1. Do is confused with the concept of passing by reference and the concept of reference type. These concepts is not the same.
    2. 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.
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.