Reputation: 261
Can anyone explain why the compiler gives an error for the following?
namespace Sandbox
{
internal class DataObj { }
public class A
{
protected DataObj _data;
}
}
Here is the compiler error.
Inconsistent accessibility: field type 'Sandbox.DataObj' is less accessible than field 'Sandbox.A._data'.
I would expect this error only if I derive from A in a different assembly. Thoughts?
Upvotes: 2
Views: 109
Reputation: 887225
The problem is that is is possible to derive from A
in a different assembly. Such a class would be unable to access the property's return type.
Therefore, the declaration itself is illegal.
In other words, this error occurs on the declaration side, not the consumption side.
The underlying philosophy here is that it should be impossible to create something which will sometimes be impossible to use.
Note that there are some exceptions to this philosophy; you can do evil tricks with generics that are impossible to use or inherit from in some situations.
This is allowed because moving those errors to the declaration side would be too restrictive.
Upvotes: 7