How to Resolve Error "CS0719: Array elements cannot be of static type" in C#
The Compiler Error CS0719 is a type system error. The message reads: "Array elements cannot be of static type 'StaticClass'".
In C#, an array is a collection of variables (memory slots). For an array like new MyClass[10] to exist, MyClass must be a type that can be instantiated or hold a value. Static classes, by definition, cannot be instantiated. You cannot create an object of a static class, so creating an array to hold such objects is logically impossible.
This guide explains why this restriction exists and how to achieve your goal, whether you intended to create objects or reference the class metadata.
Understanding Arrays and Static Classes
- Arrays: An array allocates memory to hold instances.
new T[5]creates 5 slots where instances ofT(or references toT) can be stored. - Static Classes: These are containers for global logic. They are
abstractandsealedimplicitly. You cannot writenew StaticClass().
If you try to create StaticClass[], you are asking the compiler to create an array of "things that cannot exist." Since no instance of a static class can ever be put into the array slots, the array itself is invalid.
Scenario: Attempting to Create a Static Array
This error often occurs when a developer creates a utility class marked as static, but later decides they need a list of them, realizing too late that static classes cannot be treated as data objects.
Example of error:
public static class Logger
{
public static void Log(string msg) { }
}
public class Program
{
static void Main()
{
// ⛔️ Error CS0719: Array elements cannot be of static type 'Logger'
// You cannot have an array of Logger, because you cannot have a 'new Logger()'.
Logger[] loggers = new Logger[5];
}
}
Solution 1: Make the Class Non-Static
If you need an array of these items, it implies you need instances (objects) that hold distinct state. Therefore, the class should not be static.
Solution: remove the static keyword from the class definition.
// ✅ Correct: Removed 'static'. Now acts as a standard object blueprint.
public class Logger
{
public void Log(string msg) { }
}
public class Program
{
static void Main()
{
// ✅ Correct: We can create an array of Logger objects.
Logger[] loggers = new Logger[5];
loggers[0] = new Logger();
}
}
Solution 2: Use System.Type (For Metadata)
If your goal was not to store objects, but to store a list of types (perhaps to use Reflection later, or to configure a dependency injection container), you should use an array of System.Type.
Solution: store the typeof() results.
public static class MathUtils { }
public static class StringUtils { }
public class Program
{
static void Main()
{
// ✅ Correct: We are storing the Type metadata, not instances.
Type[] utilityTypes = new Type[]
{
typeof(MathUtils),
typeof(StringUtils)
};
Console.WriteLine(utilityTypes[0].Name); // Output: MathUtils
}
}
Storing Behavior: If you wanted to store the methods inside the static class (e.g., a list of logging functions), use an array of Delegates like Action[] or Func<string, int>[] instead.
Conclusion
CS0719 prevents you from defining a collection of non-existent objects.
- Check the Class: Is it
public static class? - Determine Goal:
- Need Data Objects? Remove
staticfrom the class. - Need a List of Classes? Use
Type[]andtypeof(ClassName).
- Need Data Objects? Remove