Back to results for “Fundamentals”
C# › Fundamentals

What is the difference between out, ref and in parameters?

Intermediate

Answer

ref passes a variable by reference (must be initialised, can be read/written); out must be assigned inside the method (need not be initialised by caller); in passes by reference read-only for performance.
bool TryParse(string s, out int value);
void Swap(ref int a, ref int b);
double Length(in Vector v); // read-only
Real-world example TryParse patterns use out to return both success and the parsed value.