Sobes.tech
Middle+

What will be displayed in the console? ```csharp public class Program { static void Main() { var myClassObj = new MyClass(); var myStructObj = new MyStruct(); MethodA(myClassObj.Num); // MethodB(myStructObj); MethodC(myClassObj); MethodD(myClassObj); Console.WriteLine(myClassObj.Num); // ? Console.WriteLine(myStructObj.Num); // ? } private static void MethodA(int num) { num = num + 1; } private static void MethodB(MyStruct myStruct) { myStruct.Num += 1; } private static void MethodC(MyClass myClass) { myClass.Num += 1; } private static void MethodD(MyClass myClass) { myClass = new MyClass(); } } ```

sobes.tech AI

Answer from AI

In this code:

  • MyClass is a reference type (class), MyStruct is a value type (struct).
  • myClassObj.Num and myStructObj.Num initially equal 0 (assuming the Num field is initialized to zero).

Analysis of calls:

  1. MethodA(myClassObj.Num); — an int value (0) is passed by value, a local copy is increased inside the method, the original remains unchanged.

  2. MethodB(myStructObj); — the struct is passed by value, a copy is modified inside the method, the original remains unchanged.

  3. MethodC(myClassObj); — a reference to the object is passed, inside the method the Num field is increased by 1, the original object is modified.

  4. MethodD(myClassObj); — the parameter myClass is a local copy of the reference, inside the method the reference is reassigned to a new object, but this does not affect the external object.

Result:

  • myClassObj.Num increased by 1 in MethodC.
  • myStructObj.Num remains 0.

Console output:

1
0