简体   繁体   中英

C# Return type error

So here is my code:

public class landen
{
    public static List<Land> Lijst()
    {
        List<Land> lijst = new List<Land>
        {
            new Land("AF,AFG,Afghanistan,Islamitische Republiek AFghanistan,Kaboel,0093"),
            new Land("AL,ALB,Albanië,Republiek Albanië, Tirana,00355"),
            new Land("DZ,DZA,Algerije,Democratische Volksrepubliek Algerije,Algiers,00213")
        };
        return lijst;
    }
}

my error is:

Error CS0050 Inconsistent accessibility: return type 'List' is less accessible than method 'landen.Lijst()'

This sort of error occurs if you have a custom List class, and it is less accessible than the method itself.

Consider making your custom list public. That should solve the problem.

As the error is indicating, you are trying to return an instance of a class that has a visibility modifier - eg private, internal, protected, public - that is less accessible than your method.

As your method public static List<Land> Lijst() is public you should check for the visibility of List class.

You can only return instances of List in this case from methods that has the same or higher accessibility. Check this to know more about the restrictions when using accessibility levels:

Check this in order to know the levels of accessibility in C#.

From greater access privilege to lower:

public: Access is not restricted.

protected: Access is limited to the containing class or types derived from the containing class.

internal: Access is limited to the current assembly.

protected internal: Access is limited to the current assembly or types derived from the containing class.

private: Access is limited to the containing type.

You should have your List class defined somewhere as:

**public** class List
{
...
}

Maybe it's defined as internal or protected internal, or even doesn't have any modifier and then it's private and hence your are receiving this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM