Skip to main content

How to Resolve Error "CS0818: Implicitly-typed variables must be initialized" in C#

The Compiler Error CS0818 is a syntax error regarding the usage of the var keyword. The message reads: "Implicitly-typed variables must be initialized".

In C#, the var keyword is used for Implicit Typing. It tells the compiler: "Look at the value I am assigning to this variable, figure out its type, and treat the variable as that type forever."

Because the type is derived entirely from the assigned value, you must assign a value at the exact moment you declare the variable. You cannot declare a var variable and assign it later, because the compiler needs to know the type immediately during compilation.

This guide explains how var works and how to fix this initialization error.

Understanding Type Inference

When you write:

var number = 10;

The compiler sees the literal 10 (an int) and compiles the code as:

int number = 10;

If you write:

var number;

The compiler has no information. It doesn't know if number should be an int, a string, or a List<string>. Unlike dynamic languages (like JavaScript), C# variables must have a concrete type at compile-time. Without an initializer, inference is impossible.

Scenario 1: Declaration Without Assignment

This error commonly occurs when developers want to declare a variable at the top of a method scope and populate it later (e.g., inside an if block or loop).

Example of error:

public void ProcessData(bool condition)
{
// ⛔️ Error CS0818: Implicitly-typed variables must be initialized
// The compiler asks: "What type is 'result'?"
var result;

if (condition)
{
result = "Success";
}
else
{
result = "Failure";
}
}

Solution 1: Assign a Value Immediately

If you want to keep using var, you must provide a value on the same line. Even a default value works, provided it isn't null (see CS0815).

Solution:

public void ProcessData(bool condition)
{
// ✅ Correct: The compiler sees "Default", so 'result' becomes a 'string'.
var result = "Default";

if (condition)
{
result = "Success";
}
}

Solution 2: Use Explicit Typing

If you do not have a meaningful initial value (e.g., you want it to start as null or be unassigned until logic runs), you simply cannot use var. You must explicitly state the type.

Solution: replace var with the specific class name or primitive type.

public void ProcessData(bool condition)
{
// ✅ Correct: We explicitly tell the compiler this is a 'string'.
// It is okay to leave explicit types uninitialized (initially).
string result;

if (condition)
{
result = "Success";
}
else
{
result = "Failure";
}

System.Console.WriteLine(result);
}
note

out Parameters: Historically, you had to declare variables before passing them as out. Old: var x; Method(out x); // Error CS0818 Fixed: int x; Method(out x); Modern C#: Method(out var x); // Valid (Inline declaration)

Conclusion

CS0818 is the compiler saying: "I can't guess the type if you don't give me a clue."

  1. Check the Declaration: Are you writing var x;?
  2. Option A: If you know the value, assign it immediately: var x = 10;.
  3. Option B: If you don't know the value yet, use the explicit type: int x;.