Difficulty: Medium
Correct Answer: 20 10
Explanation:
Introduction / Context:Structs are value types in C#. Passing them by value creates a copy. Modifying the copy inside a method does not affect the original variable in the caller.
Given Data / Assumptions:
Concept / Approach:Because y is a by-value copy of x, changes to y.i do not propagate back to x. Therefore, fun prints 20, and after returning, Main still sees x.i = 10.
Step-by-Step Solution:
Inside fun: y is a copy → set y.i = 20 → Console.Write prints 20 and a space.Back in Main: x.i remains 10 → Console.Write prints 10 and a space.Combined output: "20 10".Verification / Alternative check:Change the method signature to static void fun(ref Sample y) and observe the different result (would print "20 20").
Why Other Options Are Wrong:
Common Pitfalls:Forgetting the distinction between structs (value types) and classes (reference types) when passing to methods.
Final Answer:20 10
Discussion & Comments