[Solved] inaccessible due to its protection level C# [duplicate]


“Protection Level” refers to the accessibility of a particular class member to contexts outside of that class.

The types of protection level available in C# are private, protected, internal, protected internal, and public.

Protection level is specified when you write the class. See below:

class Class1
{
    private int privInt;
    protected int protInt;
    public int pubInt;
}

The Private member is only accessible from inside that class. So if I expand the class with a function like this:

class Class1
{
    private int privInt;
    protected int protInt;
    public int pubInt;

    public int getPrivInt()
    {
        return privInt;
    }
}

I can access it. But if I try to access it from outside the class, like this:

var v = new Class1();
int i = v.privInt;

that code will fail.

If I were to mark it as public instead, then the above code would work.

What is happening in your case is that the member you are trying to access is using a protection level which makes it inaccessible to the other class. It is likely either private or protected.

Note that if you do not specify a protection level when defining a member, it is private by default.

In order for your code to work, you will either need to mark your member as public (usually bad practice) or encapsulate the access to that member using a function or a property which is marked public (usually good practice).

solved inaccessible due to its protection level C# [duplicate]