Passing a List into a method, modify the list within the method without affecting 'original'

10,714

Solution 1

List is a reference type so when you pass myPassedList as an argument to doSomething you are modifying the original list.

You have two options, either call ToList() or create a new list, as an example:

public void doSomething (List<int> myPassedList) 
{
    List<int> newList = myPassedList.ToList();
    int A = 5;
    newList.Add(A);
    //... And then some other cool code with this modified list
}

The original list myList will then only return 1 and 2.

Solution 2

If you write a method that works with a list but will not modify that list, then you should document this by code with

public void doSomething ( IEnumerable<int> myPassedValues ) 
{
    List<int> newList = myPassedValues.ToList();
    int A = 5;
    newList.Add(A);
    //... And then some other cool code with this modified list
}

Now you and all others will know, just by reading the declaration that the passed list will not be modified in this method.

Solution 3

Inside your doSomething() method, create a duplicate list by:

var newList = new List<int>(myPassedList);
int A = 5;
newList.Add (A);

myPassedList will not be affected

Share:
10,714
Jim
Author by

Jim

Updated on June 19, 2022

Comments

  • Jim
    Jim almost 2 years

    Sorry if the subject seems vague, I tried summing it up as best I can without knowing the exact terminology of what I'm trying to achieve.

    Essentially I have a list and then I call a method

    public List<int> myList;
    
    void Start () {
        myList = new List<int>();
        myList.Add (1);
        myList.Add (2);
    
        doSomething(myList);
    
        foreach (int i in myList){
            print (i);
        }
    }
    

    In my method I'd like to do this (for example)

    public void doSomething (List<int> myPassedList) 
    {
    
        int A = 5;
        myPassList.Add (A);
        //... And then some other cool code with this modified list
    }
    

    However, I dont want the original list changed, I want it exactly as it was. Essentially when I pass the list into the method I'd like a duplicate of the list, which is then made new each time the method is called.

    I want to see the console print '1' then '2'

    but it will print '1', '2' and '5'

    Hopefully this all makes sense! Thanks very much in advance for any help

    Jim

  • Callum Bradbury
    Callum Bradbury over 8 years
    It's worth noting that for reference types this new list will contain the objects from the original list, so any modifications will be done to both lists' object
  • Rob
    Rob over 8 years
    This is still a pointer to the original list, as I pointed out in the other answer
  • Jim
    Jim over 8 years
    Hi Rufo, thanks very much for your reply. Could explain why IEnumerable would help me know that the list will not be modified?
  • Jim
    Jim over 8 years
    Thank you for the answer! Do you know why passing a variable into a method won't change the original but lists will? Is it because lists are references to something?
  • Sir Rufo
    Sir Rufo over 8 years
    Because you only guarantee that you will receive an IEnumerable<int> and that cannot be modified. You should use only the minimum type you need to do your work. It is safer and you can handle anything that inherits from/supports that type/interface.
  • Darren
    Darren over 8 years
    @Jim - no problem. Look up reference types and value types. Lists are reference types and you can have multiple variables pointing to the same object (Which is what happened in your case), value types will make another copy of that variable and allow you to modify it without affecting the original.