Well, when you try to modify property of some class instance you don't even need ref
because you're modifying instance rather then reference to it.
In this example you don't need ref
keyword as you modify property :
class MyClass
{
public int MyProperty { get; set; }
}
static void Method(MyClass instance)
{
instance.MyProperty = 10;
}
static void Main(string[] args)
{
MyClass instance = new MyClass();
Method(instance);
Console.WriteLine(instance.MyProperty);
}
Output : 10
And here you do need ref
keyword because you work with reference and not with instance :
...
static void Method(MyClass instance)
{
// instance variable holds reference to same object but it is different variable
instance = new MyClass() { MyProperty = 10 };
}
static void Main(string[] args)
{
MyClass instance = new MyClass();
Method(instance);
Console.WriteLine(instance.MyProperty);
}
Output: 0
It's same for your scenario, extension methods are the same as normal static methods and if you create new object inside method then either you use ref
keyword (it's not possible for extension methods though) or return this object otherwise the reference to it will be lost.
So in your second case you should use :
public static List<T> Method<T>(this List<T> list, Func<T, bool> predicate)
{
return list.Where(predicate).ToList();
}
List<int> ints = new List<int>(new int[] { 1, 2, 3, 4, 5 });
ints = ints.Method(i => i > 2);
foreach(int item in ints) Console.Write(item + " ");
Output : 3, 4, 5