using System; /// /// A simple value type used for testing ldelema. /// public struct TestStruct { public int Value; } public class Program { /// /// Modifies a TestStruct instance by reference. Calling this with an array element /// (e.g., `ModifyStruct(ref array[i], ...)` ) will cause the C# compiler to /// generate an `ldelema` instruction. /// /// A reference to the TestStruct to modify. /// The new value to assign. public static void ModifyStruct(ref TestStruct s, int newValue) { s.Value = newValue; } /// /// Modifies a string reference. /// /// A reference to a string variable. /// The new string to assign. public static void ModifyStringRef(ref string s, string newValue) { s = newValue; } /// /// Main entry point for the ldelema test. /// /// 0 if all tests pass, otherwise a non-zero error code. public static int Main(string[] args) { // --- Test 1: Modifying a value type element in an array --- TestStruct[] structArray = new TestStruct[5]; structArray[2].Value = 100; // This call should generate an `ldelema` instruction to get the address of structArray[2]. ModifyStruct(ref structArray[2], 999); if (structArray[2].Value != 999) { return 301; // Unique error code for this test } // --- Test 2: Modifying a reference type element in an array --- string[] stringArray = new string[] { "alpha", "beta", "gamma" }; // This call should also generate an `ldelema` instruction. ModifyStringRef(ref stringArray[1], "zeta"); if (stringArray[1] != "zeta") { return 302; // Unique error code for this test } return 0; // Success } }