ref list和normal list之间有什么区别吗?

时间:2018-06-01 05:54:34

标签: c#

static void Main(string[] args) 
{
    List < Person > person = new List < Person > ();
    person.AddRange(new List < Person > {
        new Person { Name = "1" },
        new Person { Name = "2" },
        new Person { Name = "3" },
        new Person { Name = "4" },
    });    

    Program.cha(ref person);
    Program.change(person);
    Console.ReadLine();
}

class Person 
{
   public string Name { get; set; }
}

static void change(List < Person > list) 
{
   list.ForEach(x => Console.WriteLine(x.Name));
}

static void cha(ref List < Person > list) 
{
   list.ForEach(x => Console.WriteLine(x.Name));
}

change(List<Person> list)和之间的确切区别是什么? cha(ref List<Person> list)。我很好奇,如果这两者之间有任何特别的区别。

2 个答案:

答案 0 :(得分:3)

点击此处:Reference type modification vs change of reference

在您的代码中没有区别。如果您要同时分配新的List<>,则会有所不同。

示例

void foo(ref List<person> list)
{
    // Ref make sense when you are changing memory location
    // Like in below creating new list and assigning change memory location
    list = new List<person>(); // Here ref make sense, as you are creating new a list all together
}

但如果你这样做:

void foo(ref List<person> list)
{
    // ref in this function argument doesnt make sense as you are not modifying list, 
    // you are just adding, updating, delete items in it i.e modifying list 
    list.Add(new Person() {}); 
}

答案 1 :(得分:1)

明白这一点:

What's the difference between passing by reference vs. passing by value?

调试下面的代码,你会有更好的想法。

<p>Selected:</p>
<ul>
  <li *ngFor="let i of shoes.selectedOptions.selected">
    {{ i.value }}
  </li>
</ul>

虽然将这些方法作为不同的案例,

static void Main(string[] args)
{
    int num = 10;
    MethodWithoutRef(num);  //adding 2 in number [Pass by value]
    Console.WriteLine(num); //still number at caller side not changed
    //Output here : 10


    Method(ref num);        //adding 2 in number (here number is passed by ref) 
    Console.WriteLine(num); //number at caller side changed
    //output here : 12


    List<int> numList = new List<int>() { 10 };
    //List is default passed by ref, so changes in method will be 
    //      reflected at caller side even without using 'ref' keyword
    MethodWithoutRef(numList);        //[Pass by reference]
    Console.WriteLine(numList[0]);
    //output here : 12 and also, count of list has increased too


    numList = new List<int>() { 10 };
    //passing List with 'ref' doesnt make any differece in comparision with
    //      passing it without 'ref'
    Method(ref numList);
    Console.WriteLine(numList[0]);
    //output here : 12 and also, count of list has increased too

    numList = new List<int>() { 10 };
    //passing List without ref in such method, 
    //  where it creates new list out of it
    MethodWithoutRefWithNewList(numList);
    Console.WriteLine(numList[0]);
    //output here : 10 and count of list is not changed

    numList = new List<int>() { 10 };
    //passing List without ref in such method, 
    //  where it creates new list out of it
    MethodWithNewList(ref numList);
    Console.WriteLine(numList[0]);
    //output here : 12 and count of list has increased too
}

编辑: Pranay在他的回答中提到了一个有趣的观点。

相关问题