C # List passed as parameter value change

C # List passed as parameter value change

First, the example shows

Copy the code

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("");

            List<string> list1 = new List<string>();
            Test1(list1);
            Console.WriteLine(" list1:" + list1.Count()); // 总数量为 1

            Console.WriteLine("");
            Console.WriteLine("---- 亮瞎眼的分割线 ----"); 
            Console.WriteLine(""); 

            List<string> list2 = new List<string>();
            Test2(list2);
            Console.WriteLine(" list2:" + list2.Count()); // 总数量仍为 0

            Console.WriteLine("");
        }

        static void Test1(List<string> list)
        {
            list.Add("1");
            Console.WriteLine(" Test1():" + list.Count()); // 总数量为 1
        }

        static void Test2(List<string> list)
        {
            List<string> list2 = new List<string>();
            list2.Add("1");

            list = list2;
            Console.WriteLine(" Test2():" + list.Count()); // 总数量为 1
        }
    }

Copy the code

It can be found:

After Test1, the number of elements of the list from 0 to 1, and

After Test2, the number of elements in list or zero.

Second, explain

1.list type is a reference type

2. The reference itself is similar to a "save the address of the value of the variable"
So passed a reference to the method in method from the outside, then the reference itself is in fact a copy to copy to the method in use, but said this reference copy of a copy and the contents of references before (that is, the object pointed to memory address) are the same, so by the time data manipulation object reference, you can see two references to the operation of the same object; but if you are modifying a copy of the reference the value of the content itself (the reference points to the memory address of a new object), then before will not affect the referenced outside the method, so the changes will find two different reference points to the object

and if the object reference parameter before adding the ref, then the method parameter passed is no longer a reference copy, but cited the address (ie find the address referenced by reference, read the referenced memory address value in preservation, then according to the address the real values to find an object to be manipulated), so if you go at this time to change this value reference, the address will be cited To the outside of the reference method, then modify its contents, it will be found that would point to the new reference objects outside the method

There are three sections of code 3

You can look, taste:

(1)

Copy the code

List <String> = new new List List <String> (); 
ModifyList (List); 
Console.WriteLine (list.Count) 
 
Private void ModifyList (List <String> List) 
{ 
   // List here is already a copy of the reference is the , but the memory address pointed to the original method is still outside the object, so the back of the Add method with reference to the operation, is still outside of the original method of memory data objects 
    list.add ( ". 1"); 
    list.add ( "2"); 
    list.add ( ". 3"); 
}

Copy the code

(2)

Copy the code

List <String> = new new List List <String> (); 
ModifyList (List); 
Console.WriteLine (list.Count) 
 
Private void ModifyList (List <String> List) 
{ 
    List = new new List <String> (); // here the reference point is already a new memory address, the subsequent operation is the Add operation of memory data of a new object, and the object is actually outside the original method is not affected by 
    list.add ( ". 1"); 
    list.add ( "2"); 
    list.add ( ". 3"); 
}

Copy the code

(3)

Copy the code

List <String> List = new new List <String> (); 
List <String> Copy = List; // copy a reference 
ModifyList (ref List); 
Console.WriteLine (copy.Count) // copy the reference is still pointing to the original the earliest that List 
Console.WriteLine (list.Count) // List this reference has been modified in ModifyList method, the point is to ModifyList new method in the new objects out of the 
 
private void ModifyList (ref List <string > list) 
{ 
    List = new new List <String> (); // because REF, so there is already a reference to the methods that the original point is also a new memory address, the subsequent operation is the operation memory Add new data object, and references to the methods that the original is also directed to this new object 
    list.add ( ". 1"); 
    list.add ( "2"); 
    list.add ( ". 3"); 
}

Copy the code

 

 

reference:

http://www.cftea.com/c/2013/01/5724.asp

http://bbs.csdn.net/topics/390600826

Author: feel good side

Published 17 original articles · won praise 224 · views 290 000 +

Guess you like

Origin blog.csdn.net/cxu123321/article/details/105069592