Skip to main content

How to Resolve Error "CS0572: 'type' : cannot reference a type through an expression" in C#

The Compiler Error CS0572 is a syntax error regarding member access rules. The message reads: "'Type' : cannot reference a type through an expression; try 'path_to_type' instead".

This error occurs when you attempt to access a Type (such as a nested class, struct, enum, or delegate) using an instance variable (an object) rather than the class name. In C#, types and static members belong to the class definition itself, not to specific objects created from that class. You must access them statically.

This guide explains the difference between accessing members via instances versus class names.

Understanding Type Access

In C#, if you define Class B inside Class A:

  • Hierarchy: B is a nested type of A.
  • Access: To use B, you must refer to it as A.B.

You cannot create an object var obj = new A(); and then refer to the inner type as obj.B. The object obj contains data (fields/properties), but it does not contain the definition of Class B.

note

C# vs. Java/C++: Some languages allow accessing static members or nested types via instance variables. C# strictly forbids this to avoid ambiguity.

Scenario 1: Accessing Nested Classes via Instances

This is the most common cause. You instantiate the outer class and try to use that variable to create an instance of the inner class.

Example of error

public class Outer
{
public class Inner
{
public void SayHello() => System.Console.WriteLine("Hello");
}
}

public class Program
{
static void Main()
{
Outer myOuterObject = new Outer();

// ⛔️ Error CS0572: 'Outer.Inner': cannot reference a type through an expression;
// try 'Outer.Inner' instead.
// You are trying to find the definition of 'Inner' inside the variable 'myOuterObject'.
var myInner = new myOuterObject.Inner();
}
}

Solution: Use the Class Name

Ignore the variable myOuterObject. Refer to the class Outer directly.

public class Program
{
static void Main()
{
Outer myOuterObject = new Outer();

// ✅ Correct: Access the type via the Class Name
var myInner = new Outer.Inner();

myInner.SayHello();
}
}

Scenario 2: Accessing Enums Defined Inside Classes

If you define an enum inside a class to keep related constants together, you must access that enum using the class name.

Example of error

public class Robot
{
public enum State { Idle, Active }
public State CurrentState { get; set; }
}

public class Program
{
static void Main()
{
Robot r2d2 = new Robot();

// ⛔️ Error CS0572: 'Robot.State': cannot reference a type through an expression.
// 'r2d2' is an object. 'State' is a type definition.
if (r2d2.CurrentState == r2d2.State.Idle)
{
// ...
}
}
}

Solution: Use the Class Name

Access the enum through Robot.

public class Program
{
static void Main()
{
Robot r2d2 = new Robot();

// ✅ Correct: Robot.State.Idle
if (r2d2.CurrentState == Robot.State.Idle)
{
System.Console.WriteLine("Robot is waiting.");
}
}
}

Conclusion

CS0572 is the compiler enforcing the separation between definitions and instances.

  1. Check the Syntax: Are you writing variable.TypeName?
  2. Identify the Type: Is the thing after the dot a class, struct, or enum?
  3. The Fix: Replace the variable name (e.g., myObj) with the class name (e.g., MyClass).