Detailed parsing of value passing and reference passing in C #

Source: Internet
Author: User
Tags shallow copy

I. Passing parameters
Parameters can be passed either by value or by reference. Passing parameters by reference allows function members (methods, properties, indexers, operators, and constructors) to change the value of the parameter and maintain the change.

Passing Value type parameters
A value type variable contains its data directly, unlike a reference type variable, which contains a reference to its data. Therefore, passing a value type variable to a method means passing a copy of the variable to the method. A change to a parameter that occurs within a method has no effect on the raw data stored in the variable. If you want the method you called to change the value of the parameter, you must pass it by reference with a ref or out keyword. For simplicity's sake, the following example uses Ref.

1. Passing value types by value:

Class Passingvalbyval
{
static void SquareIt (int x)
//The parameter x is passed by value.
Changes to x would not affect the original value of X.
{
x *= x;
System.Console.WriteLine ("The value inside: {0}", x);
}
static void Main ()
{
int n = 5;
System.Console.WriteLine ("The value before calling the method: {0}", n);  
 SquareIt (n); Passing the variable by value.
System.Console.WriteLine ("The value after calling: {0}", n);
}
 
Variable n is a value type, containing its data (value 5). When SquareIt is invoked, the contents of n are copied into the parameter x, and the argument is squared within the method. In Main, however, the value of n is the same before and after the SquareIt method is invoked. In fact, changes that occur within a method affect only local variables X

2. Passing value types by reference
The following example is the same as the previous example in addition to passing parameters using the REF keyword. The value of the parameter changes after the method is invoked

Class Passingvalbyref
{
static void SquareIt (ref int x)
//The parameter x is passed by reference.
Changes to X would affect the original value of X.
{
x *= x;
System.Console.WriteLine ("The value inside: {0}", x);
}
static void Main ()
{
int n = 5;
System.Console.WriteLine ("The value before calling the method: {0}", n);

SquareIt (ref N); Passing the variable by reference.
System.Console.WriteLine ("The value after calling of the method: {0}", n);
}


In this example, the pass is not a value of N, but a reference to N. Parameter x is not an int type, it is a reference to int (in this case, a reference to N). Therefore, when squaring an X in a method, the actual square is the item that x refers to: N.

3. Exchange value type
A common example of changing the value of a passed parameter is the Swap method, in which the X and y two variables are passed, and the methods are exchanged for their contents. Parameters must be passed to the Swap method by reference, otherwise the local copy of the parameter will be processed within the method. The following is an example of a Swap method that uses reference parameters:

static void Swapbyref (ref int x, ref int y)
{
    int temp = x;
    x = y;
    y = temp;
}

iii. passing Reference type parameters
A variable of a reference type does not directly contain its data, it contains a reference to its data. When passing a parameter of a reference type by value, it is possible to change the data that the reference points to, such as the value of a member of a class. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for the new class and keep it out of the block. To do this, you use the ref or out keyword to pass the parameter. For simplicity's sake, the following example uses Ref.

1. Passing reference types by value
The following example shows a parameter arr that passes a reference type by value to the change method. Because this parameter is a reference to ARR, it is possible to change the value of the array element. However, when an attempt is made to reassign a parameter to a different memory location, the operation is only valid within the method and does not affect the original variable arr.

Class Passingrefbyval
{
static void change (int[] parray)
{
parray[0] = 888;//This change affects the Ori ginal element.
Parray = new Int[5] {-3,-1,-2,-3,-4}; This is local.
System.Console.WriteLine ("Inside The" method, the "the", the "the", ",", Parray[0]);

static void Main ()
{
int[] arr = {1, 4, 5};
System.Console.WriteLine ("Inside Main, before calling the" method, the "the", "the": {0} ", arr [0]);

Change (arr);
System.Console.WriteLine ("Inside Main, after calling of the method, the" the "the" the ",", "": {0} ", arr [0]);
}

In the previous example, the array was arr as a reference type and passed to the method without using the ref parameter. In this case, a copy of the reference to the ARR is passed to the method. The output display method may change the contents of an array element, in this case, from 1 to 888. However, using the new operator within the change method to allocate a fresh portion of memory will cause the variable Parray to refer to the new array. Therefore, any subsequent changes will not affect the original array arr (which is created within Main). In fact, this example creates two arrays, one inside Main and one within the change method.

2. Passing reference types by reference
This example is the same as the previous example in addition to using the REF keyword in the method header and call. Any changes that occur within a method affect the original variable in the calling program

Class Passingrefbyref
{
    static void change (ref int[] Parray)
    {
        //Both of the following changes would AFFEC t the original variables:
        parray[0] = 888;
        Parray = new Int[5] { -3,-1,-2, -3,-4};
        System.Console.WriteLine ("Inside The" method, the "the", the "the", ",", Parray[0]);

    static void Main ()
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine ("Inside Main, before calling" method, the "the", "the", ",", Arr[0]);

        Change (ref arr);
        System.Console.WriteLine ("Inside Main, after calling of the method, the" the "the", ",", ", Arr[0]);

All changes that occur within a method affect the original array in Main. In effect, the original array was reassigned using the new operator. Therefore, after calling the change method, any references to Arr will point to an array of five elements created in the change method.

3. Swap two strings
The swap string is a good example of passing reference type parameters by reference. In this example, the str1 and str2 two strings are initialized in Main and passed to the Swapstrings method as parameters modified by the REF keyword. The two strings are exchanged within the method and inside main.

Class Swappingstrings
{
static void Swapstrings (ref string s1, ref string s2)
//The string parameter is passe D by reference.
Any changes on parameters would affect the original variables.
{
String temp = S1;
S1 = s2;
S2 = temp;
System.Console.WriteLine ("Inside the method: {0} {1}", S1, S2);
}

static void Main ()
{
string str1 = "John";
String str2 = "Smith";
System.Console.WriteLine ("Inside Main, before swapping: {0} {1}", str1, str2);

Swapstrings (ref str1, ref str2); Passing strings by reference
System.Console.WriteLine ("Inside Main, after swapping: {0} {1}", str1, str2);




In this example, you need to pass parameters by reference to affect the variables in the calling program. If you remove the REF keyword from both the method header and the method call, no changes will occur in the calling program.

Four, data value delivery of reference types (copy delivery) The default of the
class creates a shallow copy of the MemberwiseClone method by creating a new object and then copying the non-static field of the current object to the new object. If the field is a value type, a bitwise copy is performed on the field. If the field is a reference type, the reference is copied but not the referenced object, so the original object and its copy refer to the same object. Deep copy, which implements the ICloneable interface. ICloneable can be used for deep and shallow copies. These are concepts, but we need to understand that:

   Class classa:icloneable
    {public
        string str;
        public subclass subclass;
        Public ClassA ()
        {
            str = "ClassA str";
            Subclass = new Subclass ();
        }
        Deep copy, multilayer not available memberwiseclone () full implementation deep copy public
        object Clone ()
        {
           //THIS.A = (string) this.a.clone ();
            this.b = (b) this.b.clone ();
            var ne = new ClassA ();
            Ne.str = This.str;
            Ne.subclass = (subclass) This.subclass.Clone (); this.b words still did not succeed return
            NE;
           return this. MemberwiseClone ();
        }    Class subclass:icloneable
    {public
        string str;
        Public subclass ()
        {
            this.str = "Subclass str";
        }
        Deep copy, because only one layer, so you can use the MemberwiseClone () method public
        Object Clone ()
        {
            this.str = (string) this.str.Clone ();
            return this. MemberwiseClone ();
        }


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.