Skip to main content

How to Resolve Error "CS0269: Use of unassigned out parameter 'parameter'" in C#

The Compiler Error CS0269 is a Definite Assignment error. The message reads: "Use of unassigned out parameter 'parameter'".

In C#, the out keyword creates a strict contract between the caller and the method. By declaring a parameter as out, the method promises to assign a valid value to that parameter before it returns. If there is any code path (like an if branch or a catch block) where the method returns without setting the value, or if you try to read the value inside the method before setting it, the compiler raises CS0269.

This guide explains the rules of out parameters and how to ensure they are correctly assigned in all scenarios.

Understanding the out Contract

  • ref: The value is initialized by the Caller. The method can read it immediately or modify it.
  • out: The value is considered uninitialized. The Method is responsible for creating the value.

Because the variable passed as out might contain garbage data (or null) when entering the method, C# forbids you from:

  1. Reading it before writing to it.
  2. Returning from the method without writing to it.

Scenario 1: Conditional Assignment (The "If" Trap)

This is the most common cause. You assign a value to the out parameter inside an if block, but you forget to assign a value in the else case. The compiler notices that if the condition is false, the variable remains unassigned.

Example of error

public class Parser
{
public void ParseData(string input, out int result)
{
if (int.TryParse(input, out int parsed))
{
result = parsed;
// If we return here, it's fine.
}

// ⛔️ Error CS0269: Use of unassigned out parameter 'result'
// If the 'if' condition fails, 'result' is never given a value
// before the method exits.
}
}

Solution: Default Assignment

You must ensure every path assigns a value. A common pattern is to initialize the parameter at the very top of the method.

public class Parser
{
public void ParseData(string input, out int result)
{
// ✅ Correct: Assign a default value immediately.
result = 0;

if (int.TryParse(input, out int parsed))
{
// Update it if successful
result = parsed;
}

// Now 'result' is guaranteed to have a value (either 0 or the parsed value).
}
}

Scenario 2: Reading Before Assigning

Because an out parameter is treated as uninitialized, you cannot use its value until you have given it one. Even if the caller passed a variable that had a value, the out keyword tells the method to ignore the incoming value.

Example of error

public class Calculator
{
public void Increment(out int val)
{
// ⛔️ Error CS0269: Use of unassigned out parameter 'val'
// You cannot read 'val' (val + 1) because we haven't assigned it yet.
val = val + 1;
}
}

Solution: Use ref or Overwrite

If you need to read the incoming value, you should use ref instead of out. If you must use out, you must overwrite it completely first.

Option A: Use ref (If input matters)

public void Increment(ref int val)
{
// ✅ Correct: 'ref' allows reading the input value.
val = val + 1;
}

Option B: Overwrite (If input doesn't matter)

public void Initialize(out int val)
{
// ✅ Correct: Write first, then read (if needed).
val = 1;
int x = val + 1;
}

Scenario 3: Try-Catch Blocks

If your method uses error handling, you must ensure the out parameter is assigned even if an exception occurs (unless you re-throw the exception, which aborts the return).

Example of error

public void ReadFile(out string content)
{
try
{
content = System.IO.File.ReadAllText("data.txt");
}
catch
{
// ⛔️ Error CS0269: If an error occurs, control jumps here.
// 'content' was not assigned, and we are about to exit the method.
}
}

Solution: Assign in Catch or Finally

Assign a fallback value (like null or empty string) in the catch block.

public void ReadFile(out string content)
{
try
{
content = System.IO.File.ReadAllText("data.txt");
}
catch
{
// ✅ Correct: Provide a default value in case of failure
content = null;
}
}
tip

Exception Exception: If the method throws an exception, it does not need to assign out parameters, because the method never "returns" successfully to the caller.

public void DoWork(out int x)
{
throw new Exception("Crash"); // Valid, even though x is unset.
}

Conclusion

CS0269 ensures data integrity.

  1. Check All Paths: Ensure if, else, case, and catch blocks all result in the parameter being assigned.
  2. Initialize Early: The easiest fix is often setting param = default; at the very first line of the method.
  3. Check Keywords: If you need to read the value before writing it, change out to ref.