Skip to main content
The out keyword is a parameter modifier in C# that passes an argument by reference rather than by value. It explicitly dictates that the invoked method is responsible for assigning a value to the parameter before the method returns normally, enforcing definite assignment at the compiler level.

Syntax and Mechanics

To utilize an out parameter, both the method definition and the calling method must explicitly include the out keyword.

Core Compiler Rules

  1. Initialization: The caller is not required to initialize the variable before passing it as an out argument. If the variable is initialized prior to the call, its initial value is discarded and overwritten.
  2. Definite Assignment: The called method must assign a value to the out parameter across all possible execution paths before the method returns normally. If an execution path leaves the method by throwing an exception, the out parameter does not need to be assigned. Reading an out parameter before assigning to it within the callee results in a compiler error.
  3. Memory Allocation: Because it is passed by reference, no new memory is allocated for the parameter within the method. The method operates directly on the memory location of the variable provided by the caller.
  4. Inline Declarations and Type Inference: C# 7.0 introduced the ability to declare out variables inline at the point of invocation. This includes the highly idiomatic out var syntax, which allows the compiler to implicitly infer the variable’s type based on the method signature.
  5. Discards: If the caller does not require the value generated by the out parameter, C# 7.0+ allows the use of a discard (_) to ignore the assignment:
ProcessData(out _);