Export (0) Print
Expand All

Restrictions on Using Accessibility Levels

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
Classes The direct base class of a class type must be at least as accessible as the class type itself.
Interfaces The explicit base interfaces of an interface type must be at least as accessible as the interface type itself.
Delegates The return type and parameter types of a delegate type must be at least as accessible as the delegate type itself.
Constants The type of a constant must be at least as accessible as the constant itself.
Fields The type of a field must be at least as accessible as the field itself.
Methods The return type and parameter types of a method must be at least as accessible as the method itself.
Properties The type of a property must be at least as accessible as the property itself.
Events The type of an event must be at least as accessible as the event itself.
Indexers The type and parameter types of an indexer must be at least as accessible as the indexer itself.
Operators The return type and parameter types of an operator must be at least as accessible as the operator itself.
Constructors 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 
{
   // Fields:
   public B myField = new B();   // Error: The type B is less accessible
                                 // than the field A.myField.
   // Constants:
   public readonly B myConst = new B();   // Error: The type B is less accessible
                                          // than the constant A.myConst.

   // Methods:
   public B MyMethod() 
   {
      return new B();   // Error: The type B is less accessible
   }                    // than the method A.MyMethod.

   // Properties: 
   public  B MyProp 
   {
      set 
      {
      }
   }   // Error: The type B is less accessible than the property A.MyProp

   // Delegates:
   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."
   
   // Operators:
   public static B operator + (A m1, B m2) 
   {
      return new B();   // Error: The type B is less accessible
                        // than the operator A.operator +(A,B)
   }

   static void Main()
   {
      Console.Write("Compiled successfully");
   }
}

See Also

Access Modifiers | Accessibility Domain | Accessibility Levels | 3.5 Member access

Show:
© 2014 Microsoft