Pass by Reference
phrase
say it: “PASS by REF-er-ens” /pæs baɪ ˈrɛfərəns/
Calling a method with the caller's variable itself, by writing ref on the parameter and the argument, so the method's changes stay after the call.
What is Pass by Reference in C#?
Pass by Reference — Handing a method the caller's own variable instead of a copy, by writing ref on the parameter and on the argument. Whatever the method does to the parameter happens to the caller's variable.
int georgia = 12;
AddTouchdown(ref georgia);
Console.WriteLine(georgia); // 18: the method changed georgia itself
void AddTouchdown(ref int score)
{
score = score + 6;
}
In a sentence
- “With pass by reference, AddTouchdown(ref georgia) leaves georgia at 18.”
Level: Apprentice · More C# terms at this level
Updated