D
dylan.miller
I'm having trouble understanding the internal access modifier. There
are many classes in my assembly that should not be accessible outside
of the assembly. I've used the internal access modifier for these
classes. I have public classes that use the internal classes, but I
get a CS0051 error if I use an internal class as a parameter to a
protected function of a public class. For example:
internal class InternalClass
{
}
public class PublicClass
{
protected void ProtectedFunction(InternalClass internalClass)
{
}
}
When I compile the code above, I get the following error:
error CS0051: Inconsistent accessibility: parameter type
'InternalClass' is less accessible than method
'PublicClass.ProtectedFunction(InternalClass)'
It seems like I should be able to do this, since a protected function
is hidden from the user of a class, and it seems like "hidden" code
should be able to use internal classes. I assume the error occurs
because a class could be derived from PublicClass outside of the
assembly, but I don't plan to do that. I thought the "protected
internal" access modifier might be the solution, but it means
protected OR internal, where what I want is protected AND internal.
Possible solutions:
1) Change ProtectedFunction() from protected to internal. This is not
good, because then ProtectedFunction() becomes accessible to any code
in the assembly. I want ProtectedFunction() hidden.
2) Change InternalClass from internal to public. This is not good,
because a domino effect will occur and I'll end up having to change
most of my internal classes to public to avoid the CS0051 error.
Is there a solution I'm missing, or is this just a limitation of
internal classes?
are many classes in my assembly that should not be accessible outside
of the assembly. I've used the internal access modifier for these
classes. I have public classes that use the internal classes, but I
get a CS0051 error if I use an internal class as a parameter to a
protected function of a public class. For example:
internal class InternalClass
{
}
public class PublicClass
{
protected void ProtectedFunction(InternalClass internalClass)
{
}
}
When I compile the code above, I get the following error:
error CS0051: Inconsistent accessibility: parameter type
'InternalClass' is less accessible than method
'PublicClass.ProtectedFunction(InternalClass)'
It seems like I should be able to do this, since a protected function
is hidden from the user of a class, and it seems like "hidden" code
should be able to use internal classes. I assume the error occurs
because a class could be derived from PublicClass outside of the
assembly, but I don't plan to do that. I thought the "protected
internal" access modifier might be the solution, but it means
protected OR internal, where what I want is protected AND internal.
Possible solutions:
1) Change ProtectedFunction() from protected to internal. This is not
good, because then ProtectedFunction() becomes accessible to any code
in the assembly. I want ProtectedFunction() hidden.
2) Change InternalClass from internal to public. This is not good,
because a domino effect will occur and I'll end up having to change
most of my internal classes to public to avoid the CS0051 error.
Is there a solution I'm missing, or is this just a limitation of
internal classes?