Using an internal class as type of a field marked protected intern

G

Guest

Hi everybody,

The C# compiler reports a Compiler Error CS0052 in the following situation:

I declared a type marked as "internal":

namespace MyNamespace
{

internal class MyInteralClass
{
// ...
}

/*
I tried to use this type in a public class, and I want it to be accessible
only for devired classes in the same assembly so I set its accessibility
level to protected internal.
*/

public class MyPublicClass
{
protected internal MyInteralClass MyField;

// ...
}

}

I tried to compile the source and the compiler reports the CS0052 error:

error CS0052: Inconsistent accessibility: field type
'MyNamespace.MyInteralClass' is less accessible than field
'MyNamespace.MyPublicClass.MyField'

Is it a bug? Cannot such consrtuction be used? I didn't find any details for
this situation in the MDSN Library.

Thanks a lot, best
Ãron Kolozs
 
B

Bruce Wood

It's not a bug. The error message is accurate.

The problem is that you are misinterpreting what "protected internal"
means. I gather from the way you phrased your post that you believe
that the two keywords have an "and" relation: that is, "protected"
further restricts "internal" so that the field is available only to
child classes within the same assembly. This is not what "protected
internal" means.

"protected internal" means that the field is available to _all_ classes
within the same assembly, _and_ any child classes, whether they are
within the same assembly or not. So, some class _outside_ your assembly
could declare itself:

public class SomeOtherClass : MyPublicClass

and it would have access to "MyField". "protected internal" is _less_
restrictive than either "protected" or "internal" alone: in effect, the
two access modifiers have an "or" relation, not an "and" relation.

So, in my example above, SomeOtherClass would have access to MyField
(because it's protected internal, and SomeOtherClass is a child class
of MyPublicClass even though it's in a different assembly), but would
not have access to MyInternalClass (because it's declared as strictly
internal to your assembly, and SomeOtherClass is in a different
assembly). Therefore, the access modifiers are inconsistent.

Change MyField to be "internal" only, and the message will go away. You
might as well, since there is no access modifier in C# to do what you
want (only child classes within the same assembly).
 
G

Guest

First of all, thank a lot for clearing up. I have dropped the "protected"
declaration from the front of the "interal" and then the source was compiled
successfully.

You're right, I was confused, because the "or" is escaped my attention. I
read the "Accessibility levels" chapter again and according to your
interpretation I realised the "or".

On reflection, we have to admit that the "protected" _and_ "internal"
declaration has no reason, because a developer has to use their own classes
correctly and no need for self restriction and other mazohism :)

Best,
Kolozs Ãron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top