C# Error CS0054 – Inconsistent accessibility: indexer return type ‘type’ is less accessible than indexer ‘indexer’

C# Compiler Error

CS0054 – Inconsistent accessibility: indexer return type ‘type’ is less accessible than indexer ‘indexer’

Reason for the Error

You would receive this error when you have a indexer defined in a class where the type of the indexer is not publicly accessible.

For example, try compiling the below code snippet.

class Student
{
}

public class DeveloperPublish
{
    public Student this[int i]   // CS0054  
    {
        get
        {
            return new Student();
        }
    }

    public static void Main() { 
    
    }
}

This will result with the compiler error because the Student class is defined as private and is being used as a indexer in the DeveloperPublish class.

C# Error CS0054 – Inconsistent accessibility: indexer return type 'type' is less accessible than indexer 'indexer'

Error CS0054 Inconsistent accessibility: indexer return type ‘Student’ is less accessible than indexer ‘DeveloperPublish.this[int]’

Solution

Ensure that the type that you are using for the indexer is public. You can set the access-modifier of the Student class to public to fix this error.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Hey, wait!Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.