C# Error CS0720 – ‘{0}’: cannot declare indexers in a static class

C# Error

CS0720 – ‘{0}’: cannot declare indexers in a static class

Reason for the Error & Solution

‘static class’: cannot declare indexers in a static class

Indexers are not meaningful in static classes, since they can only be used with instances, and it is not possible to create instances of a static type.

Example

The following sample generates CS0720:

// CS0720.cs  
  
public static class Test  
{  
    public int this[int index]  // CS0720  
    {  
        get { return 1; }  
        set {}  
    }  
  
    static void Main() {}  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...