Alex Rivera | Logout

Pass List to method without modifying original list

Asked 2011-10-10T12:36:22.440
10

Is this the only way of passing a List to a method and editing that List, without modifying the original List?

class CopyTest1
{
    List<int> _myList = new List<int>();
    public CopyTest1(List<int> l)
    {
        foreach (int num in l)
        {
            _myList.Add(num);
        }
        _myList.RemoveAt(0); // no effect on original List
    }
}
Edit
Report

1 Answer

-1

You can pass an object by reference doing the following:

public static void ReferenceMethod(ref List<T> myParam) {
    ...
} 

EDIT: The question has now been clarified, the OP was after a way not to alter the original list.

answered 2011-10-10T12:45:20.553

Your Answer