What is the difference between ref, out, and in parameters in C#?
Answer
These keywords modify how arguments are passed to methods by reference rather than by value. ref: passes by reference — the method can read and write the variable. The variable must be initialized before passing. void Double(ref int x) { x *= 2; } int n = 5; Double(ref n); // n is now 10. out: the method must assign the variable before returning — the caller doesn't need to initialize it. Used for methods returning multiple values: bool success = int.TryParse("42", out int result);. in (C# 7.2+): passes by read-only reference — the method cannot modify the variable. Used for performance when passing large structs without copying: void Print(in LargeStruct s) { }. ref and out both prevent copying; in adds immutability guarantees. Use out for TryParse patterns; use in for large struct performance; avoid ref unless truly necessary.