How to Resolve Error "CS0708: cannot declare instance members in a static class" in C#
The Compiler Error CS0708 is a structural consistency error. The message reads: " 'MemberName': cannot declare instance members in a static class".
In C#, a static class is a container for global data and methods that do not belong to any specific object instance. You cannot use the new keyword to create an instance of a static class. Therefore, declaring a member (field, method, property) inside it without the static keyword makes no sense—that member would belong to an object that can never be created.
To fix this, you must either make the member static or make the class non-static.
Understanding Static vs. Instance Members
- Static Class: A class marked with
static(e.g.,public static class Utilities). It acts as a library of functions or global settings. It is sealed and abstract implicitly. - Instance Member: A standard variable or method (e.g.,
public int Count;). It requires an instantiated object (new Class()) to exist in memory.
Since a static class cannot be instantiated, an instance member inside it would be unreachable code. The compiler enforces that every member of a static class must also be marked static.
Scenario: Missing the static Keyword
This error frequently happens when you create a helper class (like generic settings or math tools) but forget to add the static modifier to one of the fields or methods.
Example of error:
public static class AppConfiguration
{
// This is fine
public static string AppName = "MySystem";
// ⛔️ Error CS0708: 'AppConfiguration.Timeout': cannot declare instance members
// in a static class.
// This field implies it belongs to an object instance, but 'AppConfiguration'
// prevents object creation.
public int Timeout;
// ⛔️ Error CS0708: Applies to methods too.
public void Save()
{
// ...
}
}
Solution 1: Make the Member Static (Utility Class)
If the class is intended to be a global utility or configuration holder (where the data is shared across the entire application), you should add the static keyword to the member.
Solution:
public static class AppConfiguration
{
public static string AppName = "MySystem";
// ✅ Correct: The field is now static, matching the class.
public static int Timeout;
// ✅ Correct: The method is now static.
public static void Save()
{
System.Console.WriteLine("Configuration saved.");
}
}
public class Program
{
static void Main()
{
// Usage: Accessed via Class Name
AppConfiguration.Timeout = 5000;
AppConfiguration.Save();
}
}
Solution 2: Make the Class Non-Static (Object Entity)
If the class is intended to represent an entity (like a User, Product, or Session) where you need multiple distinct copies of the data, the class should not be static.
Solution: remove the static keyword from the class declaration.
// ✅ Correct: Removing 'static' makes this a standard class.
// It can now have instance members.
public class UserSession
{
// Instance member (Specific to one user)
public int UserId;
// Static member (Shared by all users) is still allowed in a non-static class
public static int GlobalUserCount;
public void Login()
{
System.Console.WriteLine($"User {UserId} logged in.");
}
}
public class Program
{
static void Main()
{
// Usage: Accessed via Instance
var session = new UserSession();
session.UserId = 101;
session.Login();
}
}
Constructors: A static class cannot have an instance constructor (public MyClass()). It can only have a static constructor (static MyClass()) to initialize static data. Attempting to add a public constructor to a static class will also trigger CS0708 (or CS0710).
Conclusion
CS0708 ensures consistency in your class design.
- Check the Class Definition: Is it marked
public static class? - Determine Intent:
- If it is a helper/utility class: Add
staticto the failing field or method. - If it is an object/entity: Remove
staticfrom the class declaration so you can create instances of it.
- If it is a helper/utility class: Add