C# Error CS0050 – Inconsistent accessibility: return type ‘type’ is less accessible than method ‘method’

C# Compiler Error Message

CS0050 – Inconsistent accessibility: return type ‘type’ is less accessible than method ‘method’

Reason for the Error

You will receive the error CS0050 when you have a class or method that is usually not accessible and you are trying to return an object of that class from a method or function. A simple example of this error is when the class has no access modifier specified or is marked as private.

Let’s have a look at an example.

Below is a code snippet in C# that has a class called Student which doesnot have any access modifier specified. By default, it is private.

class Student
{
}

public class DeveloperPublish
{
    // This results with CS0050 error
    public static Student GetStudent()  
    {
        return new Student();
    }

    public static void Main() { 
    
    }
}

You are trying to return the Student object from the method GetStudent from DeveloperPublish which would result with the below error.

Error CS0050 Inconsistent accessibility: return type ‘Student’ is less accessible than method ‘DeveloperPublish.GetStudent()’ ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs

C# Error CS0050 – Inconsistent accessibility: return type 'type' is less accessible than method 'method'

Solution

To fix this issue, ensure that the class you are trying to return from the method has the right access modifier(public) instead of private.

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.