Edit

Share via


Compiler Error CS0050

Inconsistent accessibility: return type 'type' is less accessible than method 'method'

The return type and each of the types referenced in the formal parameter list of a method must be at least as accessible as the method itself. This includes type arguments of generic types used in the return type or parameters. For more information, see Access Modifiers.

Examples

The following sample generates CS0050 because no accessibility modifier is supplied for MyClass, and its accessibility therefore defaults to private:

// CS0050.cs
class MyClass // Accessibility defaults to private.
// Try the following line instead.
// public class MyClass
{
}

public class MyClass2
{
    public static MyClass MyMethod()   // CS0050
    {
        return new MyClass();
    }

    public static void Main() { }
}

CS0050 can also occur when a generic type's type argument is less accessible than the method:

// CS0050_Generic.cs
using System.Collections.ObjectModel;

internal class CeisData // Internal class
{
    public string Name { get; set; }
}

public class MyClass
{
    public static ObservableCollection<CeisData> BuildCeis()   // CS0050
    {
        return new ObservableCollection<CeisData>();
    }
}

To fix this error, make the type argument at least as accessible as the method:

// Fixed version
using System.Collections.ObjectModel;

public class CeisData // Now public
{
    public string Name { get; set; }
}

public class MyClass
{
    public static ObservableCollection<CeisData> BuildCeis()   // OK
    {
        return new ObservableCollection<CeisData>();
    }
}