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.

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...