Skip to main content

How to Resolve Error "CS0723: Cannot declare variable of static type 'type'" in C#

The Compiler Error CS0723 is a type system restriction error. The message reads: "Cannot declare variable of static type 'StaticClass'".

In C#, variables are designed to hold values (like int, double) or references to objects (like string, List<T>). A static class, however, is purely a container for global methods and data. It cannot be instantiated, and there is no such thing as a "reference" to a static class instance because instances can never exist. Therefore, declaring a variable of a static type is logically impossible.

This guide explains why this restriction exists and how to correct your code depending on your design intent.

Understanding Static Classes and Variables

When you declare MyType variableName;, you are asking the runtime to reserve memory to hold an instance of MyType (or a pointer to one).

  • Standard Class: Can be instantiated via new. A variable holds the memory address of that instance.
  • Static Class: Cannot be instantiated. It is abstract and sealed. It has no memory address to store in a variable.

Because the variable would have nothing to hold, C# forbids the declaration entirely.

Scenario 1: Using Utility Classes (Helper Pattern)

This error most commonly occurs when a developer is used to object-oriented patterns (instantiating a class to use its methods) but is working with a static utility class (like System.Math, System.Console, or a custom helper).

Example of error

Attempting to declare a variable for a class that is designed to be accessed globally.

// A static utility class
public static class FileHelper
{
public static void Write(string text) { /*...*/ }
}

public class Program
{
static void Main()
{
// ⛔️ Error CS0723: Cannot declare variable of static type 'FileHelper'.
// You generally cannot create a variable for a static class.
FileHelper helper;

// This would also fail (CS0712) because you can't use 'new':
// helper = new FileHelper();
}
}

Solution: Access Members Directly

Do not declare a variable. Access the methods and properties directly using the Class Name.

public class Program
{
static void Main()
{
// ✅ Correct: No variable needed. Access via the type name.
FileHelper.Write("Hello World");
}
}

Scenario 2: Using Data Entities (Object Pattern)

Sometimes, you do need a variable. For example, if you are defining a User, Configuration, or Session object, you need to store data specific to that instance. If you accidentally marked the class as static, you will get this error when trying to use it normally.

Example of error

Marking a data-holding class as static but trying to use it as an object.

// This was accidentally marked 'static'
public static class UserSession
{
public string UserName { get; set; }
}

public class LoginService
{
public void Login()
{
// ⛔️ Error CS0723: Cannot declare variable of static type 'UserSession'.
// We want to create a NEW session, but the class is static.
UserSession currentSession;
}
}

Solution: Make the Class Non-Static

Remove the static keyword from the class definition. This allows you to create instances, declare variables, and pass the object around.

// ✅ Correct: Removed 'static'. Now it is a blueprint for objects.
public class UserSession
{
public string UserName { get; set; }
}

public class LoginService
{
public void Login()
{
// ✅ Correct: We can now declare a variable and create an instance.
UserSession currentSession = new UserSession();
currentSession.UserName = "Alice";
}
}
note

Dependency Injection: If you are trying to inject a static class via constructor injection (e.g., public MyClass(StaticHelper helper)), this will also trigger CS0721/CS0723. The fix is the same: either remove static to make it an injectable service, or remove the parameter and use the static class directly.

Conclusion

CS0723 is the compiler reminding you that static classes are not objects.

  1. Check the Definition: Is the class defined as public static class?
  2. Determine Intent:
    • Global Utility? Don't use variables. Call ClassName.Method() directly.
    • Data Object? Remove the static keyword from the class definition so you can declare variables of that type.