C # pass by value and pass by reference

I. Introduction

  The way of passing parameters in C # can be divided into two categories, passing by value and passing by reference. If it is further subdivided according to the type of parameters, it can be roughly divided into the following four types:

  • Pass-by-value
  • Pass-by-value of reference types
  • Value-by-reference
  • Pass-by-reference

  The string type is a special reference type. Some people think that when it is passed as a parameter, its performance is inconsistent with other reference types. But looking at the essence through the phenomenon, we will analyze it in depth, and we will find that the string type is no different from other reference types in terms of passing as a parameter.

Second, the analysis of four delivery methods

  1. Value-by-value transfer

  When passing by value, the past is a copy of the value type instance.

 1         public static int Add(int i, int j)
 2         {
 3             i = i + 1;
 4             Console.WriteLine(i);
 5             return i + j;
 6         }
 7         ......
 8         static void Main(string[] args)
 9         {
10             int a = 1;
11             Add(a, 5);
12             Console.WriteLine(a);
13         }    

  The results are as follows:

  2. Reference types are passed by value

  When passing by value, the past is a copy of the reference of the reference type instance, which may not be very clear, and it is easy to cause misunderstanding. The so-called reference is a small memory area allocated on the stack, which stores the address of the reference type instance on the managed heap. When the reference type is passed by value, it actually copies its reference on the stack and then passes it to the method. This causes two references on the stack to point to the same instance on the managed heap. So this can explain why the results of the following two methods are inconsistent.

 1     class Person
 2     {
 3         public string Name { get; set; }    
 4     }
 5     ......
 6     static void ChangePerson(Person person)
 7     {
 8          person = new Person();
 9          person.Name = "Changing Name";
10          Console.WriteLine(person.Name);
11     }
12 
13     static void ChangeName(Person person)
14     {
15         person.Name = "Changing Name";
16         Console.WriteLine(person.Name);
17     }  
18     ......
19     // 引用类型的按值传递
20     Person person = new Person() { Name="Old" };
21     ChangeName(person);
22 Console.WriteLine (person.Name); 
23 
24 // Pass by reference by value 
25 person = new Person () {Name = "Old"}; 
26 ChangePerson (person); 
27 Console.WriteLine (person.Name);

The operation result is as follows:

  3. Value type passing by reference

  There is no copy operation when passing by reference. As we all know, the value type instance is allocated on the stack, so when the value type is passed by reference, the address of the instance on the stack is actually passed to了 方法。 The way.

1 public static int Add (ref int i, int j) 
 2 { 
 3 i = i + 1; 
 4 Console.WriteLine (i); 
 5 return i + j; 
 6} 
 7 ...... 
 8 // value type By reference 
 9 int a = 1; 
10 Add (ref a, 5); 
11 Console.WriteLine (a);  
The operation result is as follows:

  4. Pass-by-reference of reference types

  The pass-by-reference process of reference types is similar to that of value types. There is no copying operation, only the address of the "reference of this instance" on the stack is passed to the method.

 1     class Person
 2     {
 3         public string Name { get; set; }    
 4     }
 5     ......
 6     static void ChangeNameRef(ref Person person)
 7     {
 8         person.Name = "Changing Name";
 9         Console.WriteLine(person.Name);
10     }
11     static void ChangePerson(ref Person person)
12     {
13         person = new Person();
14         person.Name = "Changing Name";
15         Console.WriteLine(person.Name);
16     }    
17     // 引用类型的按引用传递
18     person = new Person() { Name = "Old" };
19     ChangeNameRef(ref person);
20     Console.WriteLine(person.Name);
21 
22     person = new Person() { Name = "Old" };
23     ChangePerson(ref person);
24     Console.WriteLine(person.Name);

The operation result is as follows:

PS: Some people think that passing by reference of a reference type feels no different from passing by value, and adding ref and out to a reference type is meaningless. It can be known by comparing the above calculation results, in fact, this understanding is wrong.

3. Is the string type different from other reference types in terms of parameter passing?

  The string type is a special reference type. What are its particularities? It has little to do with our subject. We only need to understand one of its characteristics: when the value of the string type changes, it is equivalent to performing other The new operation of the reference type is not accurate, and we do not need to delve into it here. ( Need to understand the particularity of the string type, please Baidu yourself! ) It is precisely because of this feature, when passing by value, the string type and other reference types will be slightly different in appearance. However, according to this characteristic of the string type, the execution of the second method of the string type by value is the same as the execution of the second method of the above reference type by value.

1 // String type is passed by value 
 2 static void ChangeStr (string aStr) 
 3 { 
 4 aStr = "Changing"; 
 5 Console.WriteLine (aStr); 
 6} 
 7 // String type is passed by reference 
 8 static void ChangeStrRef (ref string aStr) 
 9 { 
10 aStr = "Changing"; 
11 Console.WriteLine (aStr); 
12} 
13 ...... 
14 // String type passing by value 
15 string str = "Old"; 
16 ChangeStr (str ); 
17 Console.WriteLine (str); 
18 
19 // String type passing by reference 
20 str = "Old"; 
21 ChangeStrRef (ref str); 
22 Console.WriteLine (str);

The operation result is as follows:

This article is transferred from: https://www.cnblogs.com/Suanzhai/p/4052545.html

 

Guess you like

Origin www.cnblogs.com/YourDirection/p/12700948.html