Value parameters and ref/out/in
C# normally passes arguments by value: the parameter receives a copy of the value. For an array or class reference, that copied value still refers to the same object. The ref, out and in modifiers instead express different forms of access to a caller’s storage.
using System;int count = 4;IncreaseCopy(count);Console.WriteLine(count);Increase(ref count);Console.WriteLine(count);static void IncreaseCopy(int n) { n++; }static void Increase(ref int n) { n++; }The first increment affects only the parameter. The second changes the caller’s variable through the alias.
Expected output (reasoned, not executed):
45Copy versus alias
With an integer passed normally, reassigning the parameter does not alter the caller’s integer. With ref, the parameter is an alias for an initialized variable and can read or write it. Both declaration and call specify ref. A copied array reference can mutate shared elements, but assigning a new array to the local parameter leaves the caller’s reference unchanged.
Output and readonly input
An out parameter need not be initialized before the call; a method must assign it before normal return. Both sides use out. An in parameter cannot be reassigned by the method, and the modifier is usually optional at the call site. The compiler can use a temporary for some arguments; do not assume in always avoids a copy or makes an object immutable. Prefer ordinary parameters and a return value unless aliasing or multiple outputs serve the contract.
using System;int[] bins = { 3, 7 };Update(bins);Console.WriteLine(bins[0]);static void Update(int[] items){ items[0] = 9; items = new int[] { 0 }; Console.WriteLine(items[0]);}The helper prints zero from its new array. The original remains reachable through bins and its first element was changed to nine.
Expected output (reasoned, not executed):
09using System;int quotient = Split(17, 5, out int remainder);Console.WriteLine(quotient);Console.WriteLine(remainder);Console.WriteLine(Read(in quotient));static int Split(int amount, int size, out int remainder){ remainder = amount % size; return amount / size;}static int Read(in int value) => value;The division assumes a positive size and nonnegative amount. The out variable receives the remainder; the readonly input helper can read the quotient.
Expected output (reasoned, not executed):
323Common mistake and correction
Passing an array by value does not copy its elements. Do not rely on ordinary argument passing for isolation of mutable objects. Copy the data deliberately if independent storage is required. Do not use ref merely to mutate an existing array element; the ordinary reference already permits that.
Try it yourself
Write TryTake(ref int stock, int requested) returning bool. Accept stock and requested from 0 through 100. Reject requests larger than stock without changing it; otherwise subtract requested. Starting at 5, call with 3, then 4, then 0.
Next: Scope, local functions and recursion. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Value parameters and ref/out/in — knowledge check
Apply the lesson concepts to the following scenarios.
After a normal int parameter is incremented, what happens to the caller variable?
Can a ref int argument be uninitialized before a call?
What must a method do with an out parameter before normal return?
Does passing an array reference by value prevent element mutation?
What can a method with in int n do?