开发者

C# Inconsistent Accessibility (Multiple Classes)

I am getting an Inconsistent Accessibil开发者_运维百科ity problem where I have one class that contains a List of another class

namespace NS {
public class Foo
{
    public Foo()
    {
        this.bar = new List<Bar>();
    }
    private List<Bar> bar;
}
public class Bar
{
    public Bar()
    {
    }
}
}

The error is property type NS.Bar is less accessible than property NS.Foo.Bar

The structure to the application is each Foo consists of a Dynamic Array (List) of Bar. It's not ecommerce but the best example would be an eCommerce Store->Categories->Products in terms of how the data will be accessed.


I don't get any compile errors and you shouldn't either, have you tried cleaning or rebuilding the project before trying it again? Since Bar is public you should have no problem using it in any other class.


I suppose, your real code looks like this:

namespace NS
{
    public class Foo
    {
        public Foo()
        {
            this.Bar = new List<Bar>();
        }

        public List<Bar> Bar {get;set;}
    }

    internal class Bar
    {
        public Bar()
        {
        }
    }
}

Here, the problem is, that the public property Bar of the public class Foo returns a list of instances of the internal class Bar.


Make sure your "Bar" class is public. If it's not explicitly stated public, it might be considered private or protected.


Are you sure that you have pasted the correct code? Because this error usually arises when the Type used in the property is actually less accessible than a property itself. Maybe You have forgotten to put an access classifier (so it's internal by default) and you get this error. Check the code again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜