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:
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