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

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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...