3. Restrictions on the use of accessibility levels in inheritance
When declaring type, the most important thing is whether this type must "at least" with other members or type "with the same accessibility." For example, the direct base class must have the same accessibility with the derived class. The following declaration will result in compiler errors because base class baseclass is less accessibility than Myclass:
Class Baseclass {...} public class myclass: baseclass {...} // error
The following table summarizes the restrictions on the accessibility level of the declaration.
The direct base class of the context mesh type type must have the same accessibility with the class type itself. The explicit base interface of the interface interface type must have the same accessibility with the interface type itself. The return type and parameter type of the entrustment type must have the same accessibility with the delegate type itself. The type of constant constant must have the same accessibility with the constant itself. The type of field field must have the same accessibility with at least field itself. The return type and parameter type of the method method must have the same accessibility with the method itself itself. The type of property attribute must have the same accessibility with at least the property itself. The type of event event must have the same accessibility with the event itself. The type and parameter type of the indexer index must have the same accessibility with the indexer itself. The return type and parameter type of the operator operator must have the same accessibility with the operator itself. The parameter type of the constructor constructor must have the same accessibility with the constructor itself.
Example: The following example contains different types of error declarations. An annotation after each declaration indicates the expected compiler error.
Using System; Delegate Int MyDelegate (); Class B {// Defines a private function: static int myprivateMethod () {return 0;}} public class a {// field definition: public b myfield = new b (); / / Error: Type B and A field a.myfield level Difference // Constructor: public readonly b myconst = new b (); // Error: Type B is only read // method: public b mymethod () {Return New B ();} // Property: public b myprop {set {}} public static b operator (a m1, b m2) {return new b ();} static void main () {Console.Write ("Compiled successfully ");}}