Skip to main content

How to Resolve Error "CS0825: The contextual keyword 'var' may only appear within a local variable declaration" in C#

The Compiler Error CS0825 is a scope restriction error. The message reads: "The contextual keyword 'var' may only appear within a local variable declaration or in script code".

In C#, the var keyword is used for Implicit Typing. It tells the compiler: "Look at the value on the right side of the equals sign and figure out the type for me." However, C# limits this feature strictly to Local Variables (variables inside a method).

You cannot use var for:

  1. Class Fields
  2. Method Parameters
  3. Method Return Types
  4. Properties

This rule exists because class members (fields, methods, properties) define the Contract (API) of the type. This contract must be explicit and stable; it cannot depend on inferred logic from the initialization value.

Scenario 1: Declaring Class Fields with var

This is the most common cause. Developers coming from JavaScript or dynamic languages often try to define class-level variables (fields) using var. In C#, fields require an explicit type.

Example of error

Trying to let the compiler infer a field type.

public class User
{
// ⛔️ Error CS0825: The contextual keyword 'var' may only appear
// within a local variable declaration.
// Fields must have an explicit type (e.g., string, int).
private var _name = "Alice";

public void Print()
{
// This is fine, because 'localName' is local to the method.
var localName = _name;
}
}

Solution

Replace var with the specific data type (or object/dynamic if absolutely necessary, though specific types are preferred).

public class User
{
// ✅ Correct: Explicitly defined as string
private string _name = "Alice";

public void Print()
{
var localName = _name;
}
}

Scenario 2: Using var in Method Parameters

Method signatures define how other code interacts with your class. The compiler needs to know exactly what type of data to expect. var is not a type; it's a placeholder. Therefore, you cannot use it in parameters.

Example of error

Defining a method that accepts "anything".

public class Calculator
{
// ⛔️ Error CS0825: You cannot use 'var' for parameters.
// The compiler needs to generate a specific signature for this method.
public void Add(var a, var b)
{
System.Console.WriteLine(a + b);
}
}

Solution

Specify the type. If you want the method to handle any type, use Generics (<T>) or object.

public class Calculator
{
// ✅ Correct: Specific types
public void Add(int a, int b)
{
System.Console.WriteLine(a + b);
}

// ✅ Alternative: Generic (if logic supports it)
public void Display<T>(T item)
{
System.Console.WriteLine(item);
}
}

Scenario 3: Using var as a Return Type

Similarly to parameters, the return type is part of the strict contract of a method. External code needs to know exactly what type is coming back so it can assign it to a variable.

Example of error

public class DataFetcher
{
// ⛔️ Error CS0825: Return type cannot be inferred.
public var GetData()
{
return "Hello World";
}
}

Solution

Use the explicit type.

public class DataFetcher
{
// ✅ Correct: We promise to return a string.
public string GetData()
{
return "Hello World";
}
}
note

C# vs. Dynamic: If you truly need dynamic typing (where the type is resolved at runtime), C# offers the dynamic keyword. public dynamic GetData() { ... } is valid code, but it comes with performance costs and loses compile-time safety. var is NOT dynamic; var is static typing inferred by the compiler.

Conclusion

CS0825 is the compiler reminding you that C# is a Statically Typed language.

  1. Rule of Thumb: var is only for inside methods (local variables).
  2. The Fix: Whenever you see this error, simply replace var with the actual type name (e.g., int, string, List<User>).
  3. Why? Implicit typing simplifies the implementation (inside methods) but keeps the interface (class definition) strict and readable.