ToList()– does it create a new list?

Let’s say I have a class

public class MyObject
{
   public int SimpleInt{get;set;}
}

And I have a List<MyObject>, and I ToList() it and then change one of the SimpleInt, will my change be propagated back to the original list. In other words, what would be the output of the following method?

public void RunChangeList()
{
  var objs = new List<MyObject>(){new MyObject(){SimpleInt=0}};
  var whatInt = ChangeToList(objs );
}
public int ChangeToList(List<MyObject> objects)
{
  var objectList = objects.ToList();
  objectList[0].SimpleInt=5;
  return objects[0].SimpleInt;

}

Why?

P/S: I’m sorry if it seems obvious to find out. But I don’t have compiler with me now…

12 Answers
12

Leave a Comment