static void Main(string[] args) { // Cloning a reference type array produces two arrays referencing the same objects. MyClass[] mcArray = new MyClass[3] { new MyClass(), new MyClass(), new MyClass() }; MyClass[] mcClone = (MyClass[])mcArray.Clone(); mcClone[0].MyField = 10; mcClone[1].MyField = 10; mcClone[2].MyField = 10; for (int i = 0; i < mcArray.Length; i++) { Console.WriteLine("mcArray: {0}, mcClone: {1}", mcArray[i].MyField, mcClone[i].MyField); } }
static void Main(string[] args) { MyClass[] mcArray = new MyClass[4]; // Create array. for (int i = 0; i < 4; i++) { mcArray[i] = new MyClass(); // Create class objects. mcArray[i].MyField = i; // Set field. } foreach (MyClass item in mcArray) // Change the data of field. { item.MyField += 10; } foreach (MyClass item in mcArray) { Console.WriteLine(item.MyField); // Read the changed data. } }