' .foreach ()'및 ∨
-
15-11-2019 - |
문제
나는 모든 포인트를 List<T>
에서 번역하고 싶습니다.이 작업 :
for (int i = 0; i <PolygonBase.Count; ++i)
{
PolygonBase[i] = PolygonBase[i] + MousePos;
}
.
그러나 "Nofollow"> List<T>.ForEach
'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
제휴하지 않습니다 StackOverflow