[C#] Passing by value, passing by reference and immutability of string type

Value Types and Reference Types

Value types/reference types include:

  • Value type: int, double, bool, char, decimal, struct, enum
  • Reference types: string, custom class, array, collection, object, interface

The difference between the two:

  • 1. Value types and reference types are stored in different places in memory:

    Value types are stored on the stack and reference types are stored on the heap.

  • 2. When passing value types and passing reference types, the transfer methods are different;

Passing of value types

When value types are passed, what is passed is the value itself.

using System;

namespace 值传递和引用传递
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int a = 1;
            int b = a;
            b = 2;
            Console.WriteLine("a={0}",a);
            Console.WriteLine("b={0}",b);
        }
    }
}

operation result:

Memory map:

When executing int b=a;, open up a space in the stack for storing b, and pass the value in a to b. After executing b=2;, b is assigned a new value and overwrites the original value. According to the output result, after the value is passed, the value of b has nothing to do with the value of a and does not affect each other.

passing by reference

Note: The operation results passed by reference here will be different on the string type, because the string type is immutable, and the immutability of the string type will be introduced below.

When the reference type is passed, what is passed is a reference to this object (that is, the address stored in the heap).

The following uses a custom class (Person) to illustrate:

using System;

namespace 值传递和引用传递
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Person p1 = new Person();
            p1.Name = "张三";
            Person p2 = p1;
            p2.Name = "李四";
            Console.WriteLine("p1.Name={0}",p1.Name);
            Console.WriteLine("p2.Name={0}", p2.Name);
        }
        public class Person
        {
    
    
            private string _name;
            public string Name
            {
    
    
                get {
    
     return _name; }
                set {
    
     _name = value; }
            }
        }
    }
}

operation result:

Memory map:

After the p1 object is created, a storage space is opened on the heap, and the address of the storage space is placed on the stack. The address (identification) of this space in the stack is p1. When the p1 object is passed to p2, it just copies the reference to the storage in the heap (the address in the heap) and passes it to p2. After the transfer is completed, p1 and p2 both point to the same memory, so either p1 or p2 is changed Once the content is stored, the other will also change.

Immutability of the string type

First, look at the transfer of string type according to the above-mentioned "transfer of reference type":

using System;

namespace 值传递和引用传递
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            string a = "张三";
            string b = a;
            b = "李四";
            Console.WriteLine("a={0}", a);
            Console.WriteLine("b={0}", b);
        }
    }
}

operation result:

It can be seen from the running results that the transfer of the string type is different from the general reference type, because the string type is immutable:

  • When a string is reassigned, the old value is not destroyed, but a space is re-opened to store the new value.

Memory map:

After a assigns the value "Zhang San" and passes it to b, according to the "reference type transfer", a and b point to the memory of "Zhang San" (as shown in the figure: the heap address is 10001). However, after b is re-assigned as "Li Si", a space is opened in the heap to store "Li Si", the old value "Zhang San" still exists, and the heap address of the new value (as shown in the figure: the heap address is 10002) is assigned to The storage of b in the stack makes b point to "Li Si". At this point, a and b no longer point to the same heap space. Therefore, the running result is different from the general reference type.

Guess you like

Origin blog.csdn.net/ZBC010/article/details/120499224
Recommended