When you do a foreach loop, the current item (v) will be a copy of the object in the list. If it’s a class, you can modify it since the copy is a reference to the actual object. When it’s a struct, like a Vector3d, modifying that copy will not change the object inside the list. When retrieving items inside a List collection, same thing, it returns a copy of the object inside the list.
You could do something like:
var normalized = V. Select(v=> {v.Unitize(); return v;});
Thank you for your clear explanation, my knowledge of C# is not very wide. If I understand correctly the alternative syntax you propose is LINQ. The Select() method runs a foreach loop assigning to each v in V a new value which then you add to a new list. What I don’t get is the curly brackets part, because the Select method syntax on MSDN site is Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>). Is this like the curly brackets part of a foreach loop, where you would have two statements separated by ; ? Thanks again
Yes, before your reply I was using something similar but using another list:
for (int i = 0; i < V.Count; i++)
{
Vector3d v1 = new Vector3d(V[i]);
v1.Unitize();
vectors.Add(v1);
}
At the time I probably did some mistake but your reply I got back to it and it indeed works. Thanks for the link.