Skip to main content

How to Resolve Error "CS1031: Type expected" in C#

The Compiler Error CS1031 is a syntax parsing error. The message reads: "Type expected".

This error occurs when the C# compiler analyzes a statement and reaches a specific position where the language grammar strictly requires a Data Type (such as int, string, MyClass, or void), but it found something else (like a variable name, a value, a keyword, or an empty set of brackets).

This guide covers the most common syntax mistakes that trigger this error.

Understanding Where Types Are Required

C# expects a Type identifier in specific "slots" within code statements, including:

  • Inside Generic brackets: List<Type>
  • After the as or is keywords: variable as Type
  • Inside typeof(...) or default(...): typeof(Type)
  • Before a method name (Return Type): void MethodName()

If you leave these slots empty or put a value/variable there, CS1031 is raised.

Scenario 1: Incomplete Generic Definitions

This is the most frequent cause. When instantiating a generic class like a List or Dictionary, you must specify what type goes inside the angle brackets < >. Leaving them empty is invalid syntax.

Example of error

Attempting to create a List without specifying the element type.

using System.Collections.Generic;

public class Program
{
static void Main()
{
// ⛔️ Error CS1031: Type expected
// The brackets '<>' cannot be empty in a type declaration.
List<> names = new List<>();
}
}

Solution: Specify the Type

You must provide the type inside the brackets.

using System.Collections.Generic;

public class Program
{
static void Main()
{
// ✅ Correct: We specified 'string'
List<string> names = new List<string>();

// ✅ Correct: Using 'var' and Target-Typed new (C# 9.0+)
// This infers the type, but the declaration still requires <string> on one side.
List<string> moreNames = new();
}
}

Scenario 2: The as and is Operators

The as operator is used to cast an object to a specific Type. You cannot cast an object to another Variable.

Example of error

Trying to cast variable a to variable b.

public class User { }

public class Program
{
static void Main()
{
object data = new User();
User existingUser = new User();

// ⛔️ Error CS1031: Type expected
// 'existingUser' is a variable, not a class definition.
// You cannot say "Treat data as existingUser".
var result = data as existingUser;
}
}

Solution: Use the Class Name

The right-hand side of as (or is) must be a Class, Interface, or Struct name.

public class Program
{
static void Main()
{
object data = new User();

// ✅ Correct: 'User' is a Type.
var result = data as User;
}
}

Scenario 3: typeof and default Usage

The typeof operator requires the name of a class (to get its metadata). It does not accept instances (variables). Similarly, default(...) expects a type to return the default value for.

Example of error

Passing a value instead of a type.

public class Program
{
static void Main()
{
int number = 10;

// ⛔️ Error CS1031: Type expected
// 'number' is a variable. typeof() needs a class name.
var t = typeof(number);

// ⛔️ Error CS1031: '5' is a value. default() needs a type like 'int'.
var d = default(5);
}
}

Solution: Use Types or GetType()

If you have an instance and want its type, use .GetType(). If you need the metadata, pass the class name to typeof.

public class Program
{
static void Main()
{
int number = 10;

// ✅ Correct: Getting type from an instance at runtime
var t1 = number.GetType();

// ✅ Correct: Getting type metadata compile-time
var t2 = typeof(int);

// ✅ Correct: Getting default value for a type (0 for int)
var d = default(int);
}
}

Scenario 4: Missing Return Types in Methods

When defining a method, you must explicitly state what it returns. If it returns nothing, you must type void. You cannot omit the return type (unlike constructors).

Example of error

Defining a method that looks like a constructor but isn't one.

public class Calculator
{
// ⛔️ Error CS1031: Type expected
// This looks like a method named 'Add', but it has no return type.
// It is not a constructor because the name 'Add' != class name 'Calculator'.
public Add(int a, int b)
{

}
}

Solution: Add void or a Type

Specify the return type.

public class Calculator
{
// ✅ Correct: Added 'void'
public void Add(int a, int b)
{
// ...
}

// ✅ Correct: Added 'int'
public int Sum(int a, int b)
{
return a + b;
}
}

Conclusion

CS1031 is the compiler asking: "What kind of data goes here?"

  1. Check Generics: Are your angle brackets empty <>? Fill them.
  2. Check Operators: Are you using as, is, typeof, or default? Ensure the argument is a Class Name (e.g., string), not a variable name or value.
  3. Check Methods: Did you forget void or int before the method name?