Skip to content

How to Resolve Error "CS0720: cannot declare indexers in a static class" in C#

Fix the C# compiler error CS0720 by converting a static class to an instance class, using static methods, or implementing the Singleton pattern for indexer syntax.

The Compiler Error CS0720 is a structural logic error. The message reads: "'StaticClass': cannot declare indexers in a static class".

In C#, an Indexer allows an object to be indexed like an array (e.g., myObject[0]). The syntax for defining an indexer specifically uses the this keyword: public int this[int index] { ... }.

A static class, however, cannot be instantiated. It has no objects and, crucially, no this pointer. Because an indexer is inherently an instance member (it operates on a specific object instance using this), declaring one inside a static class is impossible.

This guide explains how to restructure your code depending on whether you need object-based indexing or global static access.

Understanding Indexers and Static Scope

  • Indexer Definition: public ReturnType this[IndexType i] { get; set; }
  • Static Class: Cannot create instances (new). Contains only static members.

The this keyword refers to "the current instance". Since a static class has no instances, the concept of "this" does not exist within it. Therefore, you cannot define a member that relies on this.

Scenario: The Invalid Indexer

This error often occurs when a developer wants to create a globally accessible list or dictionary wrapper and attempts to give it a convenient array-like syntax.

Example of error:

using System.Collections.Generic;

public static class GlobalCache
{
    private static Dictionary<int, string> _data = new Dictionary<int, string>();

    // ⛔️ Error CS0720: 'GlobalCache': cannot declare indexers in a static class.
    // Indexers require 'this', which static classes do not have.
    // You cannot write GlobalCache[0].
    public static string this[int key]
    {
        get { return _data.ContainsKey(key) ? _data[key] : null; }
        set { _data[key] = value; }
    }
}

Solution 1: Make the Class Non-Static (Standard Object)

If you want the convenience of syntax like myCache[0], you must create an object. This is the standard Object-Oriented approach.

Solution: remove the static keyword from the class.

using System.Collections.Generic;

// ✅ Correct: Removed 'static'. Now a standard class.
public class Cache
{
    private Dictionary<int, string> _data = new Dictionary<int, string>();

    // Indexers are valid on instance classes
    public string this[int key]
    {
        get { return _data.ContainsKey(key) ? _data[key] : null; }
        set { _data[key] = value; }
    }
}

public class Program
{
    static void Main()
    {
        var cache = new Cache();
        cache[1] = "Data"; // Syntax works
    }
}

Solution 2: Use Static Methods (Global Utility)

If the class must remain static (e.g., it is a global helper or holds application-wide settings), you cannot use indexer syntax. You must use standard static methods (Get and Set) instead.

Solution: replace this[...] with named methods.

using System.Collections.Generic;

public static class GlobalCache
{
    private static Dictionary<int, string> _data = new Dictionary<int, string>();

    // ✅ Correct: Use a static method instead of an indexer.
    public static string GetValue(int key)
    {
        return _data.ContainsKey(key) ? _data[key] : null;
    }

    public static void SetValue(int key, string value)
    {
        _data[key] = value;
    }
}

public class Program
{
    static void Main()
    {
        // Usage via methods
        GlobalCache.SetValue(1, "Data");
        var val = GlobalCache.GetValue(1);
    }
}

Solution 3: Use the Singleton Pattern

If you want the best of both worlds, Global Access (like a static class) and Indexer Syntax (like an instance), use the Singleton pattern.

The class is a normal class (so it can have indexers), but you provide a static Instance property to access it globally.

Solution:

using System.Collections.Generic;

public sealed class GlobalCache
{
    // 1. Singleton Instance
    public static readonly GlobalCache Instance = new GlobalCache();

    private Dictionary<int, string> _data = new Dictionary<int, string>();

    // 2. Private constructor prevents other instances
    private GlobalCache() { }

    // 3. Instance Indexer
    public string this[int key]
    {
        get { return _data.ContainsKey(key) ? _data[key] : null; }
        set { _data[key] = value; }
    }
}

public class Program
{
    static void Main()
    {
        // Usage: Access the static Instance, then use the indexer
        GlobalCache.Instance[1] = "Singleton Data";
        
        System.Console.WriteLine(GlobalCache.Instance[1]);
    }
}

Conclusion

CS0720 reminds you that square brackets [] on a class apply to objects, not types.

  1. Standard Instances: If you need obj[i], remove static from the class.
  2. Global Methods: If you need Class.Method(), replace the indexer with GetItem()/SetItem() methods.
  3. Singleton: If you need Class.Instance[i], use the Singleton pattern.
Navigation