What is the advantage of passing a list as parameter as ref in C#? List is not a value type so every changes done on it will reflect after returning the function.
class Program
{
static void Main(string[] args)
{
var myClass = new MyClass();
var list = new List<string>();
myClass.Foo(ref list);
foreach (var item in list)
{
Console.WriteLine(item);
}
}
}
class MyClass
{
public void Foo(ref List<string> myList)
{
myList.Add("a");
myList.Add("b");
myList.Add("c");
}
}
I can remove "ref" and it will work fine. So my question is for what usage do we need to add ref keyword for lists, arrays... Thanks