What is the difference between ref and out keywords in method parameters?
What is the difference between ref and out keywords in method parameters?
205
10-Jun-2025
ICSM Computer
12-Jun-2025In C#, both
refandoutare used to pass arguments by reference to a method, but they differ in terms of initialization requirements and intended purpose.Key Differences
refoutrefExamplexbefore passingxinside the methodoutExamplexandydon’t need initialization before the callCompile-Time Rules
refoutWhen to Use What?
refoutrefBonus:
ref readonlyYou can also use
ref readonlyto pass by reference without allowing modification.