Same code returning different results
Hey guys, what is the difference between
List<Vector2> newList = oldList;
and
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?
Answer by ByteSheep · Dec 03, 2016 at 01:47 PM
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.
Your answer
Follow this Question
Related Questions
Problem with halt with rotating on y axis 1 Answer
How to make the rotation of a child object of object A match the rotation of object B ? 0 Answers
Broken rotation (c#) 0 Answers
Get LineRenderer positions or perform a transformation to an array of Vector3s 1 Answer
Unity 2D lookat and shoot issues 0 Answers