Вопрос

Я хочу перевести все точки в генеракодицетагкоде.Это работает:

for (int i = 0; i <PolygonBase.Count; ++i) 
{
    PolygonBase[i] = PolygonBase[i] + MousePos;
}
.

Но с помощью List<T> неT:

PolygonBase.ForEach(v => v += MousePos);
.

идеи?

Это было полезно?

Решение

Your current code is simply re-assigning the local variable v to a new value - it doesn't refer back to the original value in the list. It's the equivalent of writing:

foreach(int v in PolygonBase)
{
    v += MousePos;
}

To write back to the original value, use ConvertAll:

PolygonBase.ConvertAll(v => v += MousePos);

Другие советы

ForEach is not mutable, it doesn't change the data structure in any way. Check out this threat. Using conditional lambda statements with a foreach Action on a list

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top