Error inconsistent accessibility property type is less accessible than property



[Solved] Error CS0053 Inconsistent accessibility: property type is less accessible than property

February 27, 2021

The exception “Error CS0053 Inconsistent accessibility: property type is less accessible than property” is one of the confusing exceptions we can find while programming with C# .Net. Here in this small article, I am going to explain how to get rid of this exception.

Problem

The syntax error “Error CS0053 Inconsistent accessibility: property type is less accessible than property” may be displayed in the visual studio when the user working with property types. In my case, this issue was found for the above code shown in the screenshot.

Solution

To solve the above issue I supposed to remove the public access modifier as shown below screenshot. The same problem I could observe in one of the classes in my project and I could solve the issue by just removing the public access modifier.

The reason for the issue is when the class or interface has public access modifier and the property of the class or interface has no access modifier, which means it defaults to internal, it won’t work. Property needs to have the same or higher access as a class or interface.

Vice versa of the above reason, when the class or interface has no access modifier, that means it defaults to internal and property is having public access modifier, this will not work. Because class or interface needs to have the same or higher access as property.

Conclusion

The simplest fix for this issue is to add or remove the public access modifier to the class or interface. Because as per the above explanation, this issue will occur when two different access modifiers for class/interface and property. Then one of the access modifiers is less accessible than others.

Читайте также:  Android exception in service
Sreenivasa Rangan TR

Software professional with 7+ years of experience in all phases of software development with robust problem-solving skills and proven experience in creating and designing software in a test-driven environment.

Источник

Error inconsistent accessibility property type is less accessible than property

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

I can’t figure out why my program is giving this compiler error. This is the scenario:

They are all in the same namespace, and if I put the ‘public’ access modifier in front of the ‘enum SomeEnum’ definition, the error goes away. My question is: why do I have to do that?

Any help would be greatly appreciated. Thanks.

Answers

That’s because someone else could still reference your assembly and inherit from BaseClass.

If they do that, they have access to the «en» field, but not the type of the field. If they want to change that field’s property, they’ll need to be able to have access to the field type, ie, they’ll have to have enumSomeEnum.

Make enumSomeEnum public, or make «en» internal, so it’s not accessible outside the assembly. David Morton — http://blog.davemorton.net/ — @davidmmorton — ForumsBrowser, a WPF MSDN Forums Client

All replies

You can’t make a base class less accessible than a derived class. If you’re going to expose the derived class to the outside world, via a «public» accessibility modifier, then the class it derives from must also be accessible. It’s like trying to give someone a Toyota Corolla without giving them a Car. The Toyota is a car, so when you give them one, you give them both.

Читайте также:  What is mysql error code 1064

If you’d really like to expose a class from an assembly in which it’s «base» class is not public it can be done through aggregation. There are many reasons to do this such as the Bridge pattern.

Reread what I said and it wasn’t clear on the meaning.

That’s because someone else could still reference your assembly and inherit from BaseClass.

If they do that, they have access to the «en» field, but not the type of the field. If they want to change that field’s property, they’ll need to be able to have access to the field type, ie, they’ll have to have enumSomeEnum.

Make enumSomeEnum public, or make «en» internal, so it’s not accessible outside the assembly. David Morton — http://blog.davemorton.net/ — @davidmmorton — ForumsBrowser, a WPF MSDN Forums Client

You can demonstrate to yourself why the base class is needed. Add some default parameterless constructors to your classes.

public class BaseClass
<
public BaseClass()
<
return;
>
protected enumSomeEnum en;
>

public sealed class DerivedClass : BaseClass
<
public DerivedClass()
<
return;
>
>

Now create an instance of DerivedClass .

DerivedClass dc = new DerivedClass():

. and set a breakpoint on that line. Press F11 until execution returns to the line with the breakpoint.
You will a BaseClass object get constucted! The DerivedClass «contains a» and «is a» BaseClass object.

When you cast the DerivedClass object to the BaseClass, that object you saw constructed is the resulting object.
If try the same breakpoint walk-thru to a BaseClass object you will see why you cannot cast a base class to a derived class.

Mark the best replies as answers. «Fooling computers since 1971.»

Читайте также:  Ошибка internal exception java net socketexception connection reset

Источник

Оцените статью
toolgir.ru
Adblock
detector