ToList()-- Does it Create a New List?

Posted by Ngu Soon Hui on Stack Overflow See other posts from Stack Overflow or by Ngu Soon Hui
Published on 2010-05-05T14:29:21Z Indexed on 2010/05/05 14:38 UTC
Read the original article Hit count: 202

Filed under:
|

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...

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ