Ref (C # Reference)

Source: Internet
Author: User

The ref keyword passes arguments by reference, not by value. The effect of passing by reference is that any changes to the parameters in the called method are reflected in the calling method. For example, if the caller passes a local variable expression or an array element access expression, the calling method replaces the object with the object referenced by the ref parameter, and then the caller's local variable or array element begins referencing the new object.

note

Don't confuse concepts with reference types that pass through references. These two concepts are different. ref   Modified, regardless of whether the method parameter is a value type or a reference type. when passing by reference, the value type is not boxed.

To use the ref parameter, both the method definition and the calling method must explicitly use the ref keyword, as shown in the following example.

C#
Class refexample{    static void Method (ref int i)    {        //Rest the mouse pointer over I-Verify that it's an int .        The following statement would cause a compiler error if I        //were boxed as an object.        i = i + +;    }    static void Main ()    {        int val = 1;        Method (ref val);        Console.WriteLine (val);        Output:45    }}

ref   parameters must be initialized before they can be passed. out   parameter, and you do not need to explicitly initialize the argument of the parameter until it is passed. out. " > For more information, see  out.

A member of a class cannot have a signature that differs only in the ref and out aspects. if the only difference between the two members of a type is that one has a ref parameter and the other has an out parameter, a compilation error occurs. For example, the following code will not compile.

C#
Class cs0663_example{    //Compiler error CS0663: "Cannot define overloaded     //methods that differ only on ref and O UT ".    public void SampleMethod (out int i) {} public    void SampleMethod (ref int i) {}}

However, when one method has a ref or out parameter and another method has a value parameter, the overload can be completed, as shown in the following example.

C#
Class refoverloadexample{public    void SampleMethod (int. i) {} public    void SampleMethod (ref int i) {}}

In other cases where signature matching is required (such as hiding or overriding),ref and out are part of the signature and do not match each other.

property is not a variable. They are methods that cannot be passed to the ref parameter.

For information about how to pass an array, see passing arrays with ref and Out (C # Programming Guide).

You cannot use the ref and Out keywords for several methods:

    • An async method, defined by using the async modifier.

    • The iterator method, including the yield return or yield break statement.

The preceding example shows what happens when a value type is passed by reference.You can also userefKeyword to pass a reference type.Passing reference types by reference allows the called method to replace the object in the calling method with the object referenced by the reference parameter.The storage location of the object is passed to the method by the value of the reference parameter.If you change the value in the parameter store location (to point to the new object), you can also modify the storage location to the location referenced by the caller. The following example passes an instance of a reference type as a ref parameter. For more information about how to pass reference types by value and reference, see passing reference type parameters (C # Programming Guide).

C#
Class refexample2{static void Changebyreference (ref Product Itemref) {//The following line changes the add Ress that's stored in//parameter itemref.        Because Itemref is a ref-parameter, the//address that's stored in variable item on Main also is changed.        Itemref = new Product ("stapler", 99999); You can change the value of the one of the properties of//Itemref.        The change happens to item in Main as well.    Itemref.itemid = 12345;        } static void Main () {//Declare an instance the Product and display its initial values.        Product item = new Product ("fasteners", 54321);  System.Console.WriteLine ("Original values in Main. Name: {0}, ID: {1}\n ", item. ItemName, item.        ItemID);        Send item to Changebyreference as a ref argument.        Changebyreference (ref item);  System.Console.WriteLine ("Back in Main.") Name: {0}, ID: {1}\n ", item. ItemName, item.    ItemID); }}class Product{public Product (string name, int newID) {itemname = name;    ItemID = NewID;    public string ItemName {get; set;} public int ItemID {get; set;}}  Output://original values in Main.  Name:fasteners, Id:54321//back in Main. Name:stapler, id:12345

Ref (C # Reference)

Related Article

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.