gpt4 book ai didi

c# - 使用 foreach 迭代对象列表

转载 作者:太空狗 更新时间:2023-10-30 00:18:22 26 4
gpt4 key购买 nike

我遇到了这个声明:

"When using foreach on a list of objects, the iterated object instance is not editable, but the object properties are editable"

有人可以用一个简单的例子来证明上面的内容吗?

让我重新措辞(因为我发现了两个版本的声明),也许这个声明更清楚:

"When using foreach on a list of elements, the iteration variable that provides the element is readonly, but the element properties are editable"

最佳答案

foreach(var foo in foos)
{
foo = null; // WRONG, foo is not editable
foo.name = "John"; // RIGHT, foo properties are editable
}

关于c# - 使用 foreach 迭代对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38099328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com