C# Keep Changes Made in Method Out of Scope
<p>Still virtually a beginner with C# programming - help with what is probably an elementary problem very much appreciated.</p>

<p>I have a number of objects in an application that implement a common interface.  I want to create a method that will accept any collection of objects that implement that interface, perform an operation on those objects, with the original collection having been transformed.</p>

<p>The behaviour I am seeing is that the objects are manipulated in the scope of the method external to the calling code, but not where and after the call.  I am unable to figure out how to solve this.</p>

<p>Here is a simplified version of the code I've been testing this with.</p>

<p>The interface as follows:</p>

<pre><code>public interface IDated
{
    DateTime? Date { get; set; }
}
</code></pre>

<p>An example of an object implementing that interface:</p>

<pre><code>public class MyObject : IDated
{
    public string Name { get; set; }
    public DateTime? Date { get; set; }
}
</code></pre>

<p>The method that changes the objects:</p>

<pre><code>public class Operator
{
    public void Operate(IEnumerable&lt;IDated&gt; objects)
    {
        objects = from o in objects where o.Date.HasValue select o;
    }
}
</code></pre>

<p>The calling code, where at the end no change has occurred:</p>

<pre><code>static void Main(string[] args)
    {
        var one = new MyObject("One", null);
        var two = new MyObject("Two", DateTime.Parse("30-06-06"));

        var list = new List&lt;MyObject&gt;();
        list.Add(one);
        list.Add(two);

        new Operator().Operate(list);

        Console.WriteLine(String.Format("Objects in list = {0}\r\nOperation Succeeded: {1}", list.Count.ToString(), (list.Count == 1).ToString()));
    }
</code></pre>

<p>The output is "2" and "False".  Googling this problem, I've had a try at passing the collection in as "ref" argument, as in:</p>

<pre><code>public void Operate(ref IEnumerable&lt;IDated&gt; objects)
    {
        objects = from o in objects where o.Date.HasValue select o;
    }
</code></pre>

<p>And:</p>

<pre><code>new Operator().Operate(ref list);
</code></pre>

<p>However the last bit of code has a beautiful vibrant red underline, and it reckons the argument is invalid.  I am unable to see why?</p>

<p>Again, any help greatly appreciated.</p>

<p>Cheers,</p>

<p>Tim.</p>
