Restrictions on Using Accessibility Levels (C# Reference)
When you declare a type, it is essential to see if that type has to be at least as accessible as another member or type. For example, the direct base class must be at least as accessible as the derived class. The following declarations will result in a compiler error, because the base class BaseClass
is less accessible than MyClass
:
class BaseClass {...}
public class MyClass: BaseClass {...} // Error
The following table summarizes the restrictions on using declared accessibility levels.
Context | Remarks |
---|---|
The direct base class of a class type must be at least as accessible as the class type itself. |
|
The explicit base interfaces of an interface type must be at least as accessible as the interface type itself. |
|
The return type and parameter types of a delegate type must be at least as accessible as the delegate type itself. |
|
The type of a constant must be at least as accessible as the constant itself. |
|
The type of a field must be at least as accessible as the field itself. |
|
The return type and parameter types of a method must be at least as accessible as the method itself. |
|
The type of a property must be at least as accessible as the property itself. |
|
The type of an event must be at least as accessible as the event itself. |
|
The type and parameter types of an indexer must be at least as accessible as the indexer itself. |
|
The return type and parameter types of an operator must be at least as accessible as the operator itself. |
|
The parameter types of a constructor must be at least as accessible as the constructor itself. |
Example
The following example contains erroneous declarations of different types. The comment following each declaration indicates the expected compiler error.
// Restrictions_on_Using_Accessibility_Levels.cs
// CS0052 expected as well as CS0053, CS0056, and CS0057
// To make the program work, change access level of both class B
// and MyPrivateMethod() to public.
using System;
// A delegate:
delegate int MyDelegate();
class B
{
// A private method:
static int MyPrivateMethod()
{
return 0;
}
}
public class A
{
// Error: The type B is less accessible than the field A.myField.
public B myField = new B();
// Error: The type B is less accessible
// than the constant A.myConst.
public readonly B myConst = new B();
public B MyMethod()
{
// Error: The type B is less accessible
// than the method A.MyMethod.
return new B();
}
// Error: The type B is less accessible than the property A.MyProp
public B MyProp
{
set
{
}
}
MyDelegate d = new MyDelegate(B.MyPrivateMethod);
// Even when B is declared public, you still get the error:
// "The parameter B.MyPrivateMethod is not accessible due to
// protection level."
public static B operator +(A m1, B m2)
{
// Error: The type B is less accessible
// than the operator A.operator +(A,B)
return new B();
}
static void Main()
{
Console.Write("Compiled successfully");
}
}
C# Language Specification
For more information, see the following sections in the C# Language Specification:
3.5.1 Declared Accessibility
3.5.4 Accessibility constraints
10.2.3 Access Modifiers
10.2.6.2 Declared Accessibility
10.2.6.5 Access to private and protected members of the containing type
See Also
Reference
C# Keywords
Access Modifiers (C# Reference)
Accessibility Domain (C# Reference)
Accessibility Levels (C# Reference)
Access Modifiers (C# Programming Guide)
public (C# Reference)
private (C# Reference)
protected (C# Reference)
internal (C# Reference)