Same code returning different results

Hey guys, what is the difference between

List<Vector2> newList = oldList;


List<Vector2> newList = newList<Vector2>(oldList);

If you check my last question , I used the first method and I encountered a problem however using the second method I had solved the problem.Should’nt the code work exactly the same?

In your first code snippet you are assigning the reference of oldList to newList. This means that they are both pointing to the same data and changing either one will change the others data as well.

Basically you are creating the same variable with a different name.

List<Vector2> newList = oldList;

Your second code example creates a copy of the entire list and all it’s elements, meaning that if you modify newList it won’t affect oldList.

List<Vector2> newList = new List<Vector2>(oldList);

You can check msdn for more info, for example on the various List constructors.