que tiene el código (C#):actualización Struct en bucle foreach en C#
using System.Collections.Generic;
namespace ConsoleApplication1
{
public struct Thing
{
public string Name;
}
class Program
{
static void Main(string[] args)
{
List<Thing> things = new List<Thing>();
foreach (Thing t in things) // for each file
{
t.Name = "xxx";
}
}
}
}
No va a compilar.
El error es:
Cannot modify members of 't' because it is a 'foreach iteration variable'
Si cambio Thing
a un class
en lugar de un struct
, sin embargo, que se compila.
Por favor alguien puede explicar lo que está pasando?
Pregunta relacionada http://stackoverflow.com/questions/1538301/c-does-foreach-iterate-by-reference/1538316#1538316 –
Gracias por el enlace, Brian. –