4
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));
}

What is the exact difference between change(List<Person> list) and cha(ref List<Person> list). I am just curious if there is any particular difference between these two.

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Kalp
  • 1,284
  • 2
  • 8
  • 16
  • 4
    ref is used usually when value type passing parameter in method. to get the updated value back on calling scope. `List` is pass by ref by default. so explicitly ref is not required. – Amit Jun 01 '18 at 05:56
  • 1
    In this particular example the ref is meaningless. You should probably read Jon Skeet's [Parameter passing in C#](http://www.yoda.arachsys.com/csharp/parameters.html) – Zohar Peled Jun 01 '18 at 05:57
  • in first method _reference_ to list (which is stored in stack) is copied (but not the list itself) while in second case _reference_ to list is passed as reference. As a result in second method you can do `list = new List()` – Dmitry Pavliv Jun 01 '18 at 05:58

2 Answers2

3

Check here : Reference type modification vs change of reference

In your code there is no difference. It makes a difference if you are assigning a new List<> all together.

Example

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
}

but if you do it like this:

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() {}); 
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • You don't need `ref` in the second example to modify the initial list https://dotnetfiddle.net/dR4eMH – fubo Jun 01 '18 at 06:07
  • 1
    @fubo - dont get me wrong , but i kept it to make op understand that passing ref in second example doenst make sense , so that y i kep it, if you check comment there i already wrote if you are passing ref in second example doesnt make sense – Pranay Rana Jun 01 '18 at 06:08
  • @fubo - agree with you , but its there to make OP understand it, I already wrote in comment besides it `//this doesnt make sense as you are not modifying list`..I hope you got me – Pranay Rana Jun 01 '18 at 06:09
  • 1
    @fubo - updated to make it understandable – Pranay Rana Jun 01 '18 at 06:11
1

Understand this :

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

And Debug below code, you will get better idea.

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
}

while having these methods as different cases,

static void MethodWithoutRef(int num)
{
    num = num + 2;
}
static void Method(ref int num)
{
    num = num + 2;
}
static void MethodWithoutRef(List<int> numList)
{
    numList[0] = numList[0] + 2;
    numList.Add(12);
}
static void Method(ref List<int> numList)
{
    numList[0] = numList[0] + 2;
    numList.Add(12);
}
static void MethodWithoutRefWithNewList(List<int> numList)
{
    numList = new List<int>(numList);
    numList[0] = numList[0] + 2;
    numList.Add(12);
}
static void MethodWithNewList(ref List<int> numList)
{
    numList = new List<int>(numList);
    numList[0] = numList[0] + 2;
    numList.Add(12);
}

EDIT: added an interesting point what Pranay has mentioned in his answer.

Amit
  • 1,821
  • 1
  • 17
  • 30