在C#中更新foreach循环中的struct

我有这个代码(C#):

using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing { public string Name; } class Program { static void Main(string[] args) { List things = new List(); foreach (Thing t in things) // for each file { t.Name = "xxx"; } } } } 

它不会编译。
错误是:

 Cannot modify members of 't' because it is a 'foreach iteration variable' 

但是,如果我将Thing更改为class而不是struct ,则它会编译。

请有人解释一下发生了什么?

或多或少的说法,编译器不会让你在foreach中更改(部分)循环var。

只需使用:

 for(int i = 0; i < things.Count; i+= 1) // for each file { things[i].Name = "xxx"; } 

Thing是一个类时它起作用,因为你的循环var是一个引用,你只对引用的对象进行更改,而不是对引用本身进行更改。

结构不是引用类型,而是值类型。

如果你有一个class而不是Thingstruct ,foreach循环会为你创建一个引用变量,它会指向你列表中的正确元素。 但由于它是一个值类型,它只能在你的Thing的副本上运行,在这种情况下是迭代变量。

结构是值类型,但类是引用类型。 这就是为什么它编译时这是一个类而不是它是一个结构

查看更多: http : //www.albahari.com/valuevsreftypes.aspx

我更喜欢@Henk解决方案的替代语法是这样的。

 DateTime[] dates = new DateTime[10]; foreach(int index in Enumerable.Range(0, dates.Length)) { ref DateTime date = ref dates[index]; // Do stuff with date. // ... } 

如果你在循环中做了大量的工作,那么不必在任何地方重复索引就更容易了。

PS DateTime实际上是一个非常糟糕的例子,因为它没有你可以设置的任何属性,但你得到了图片。