Deep analysis of C # inheritance mechanism 7

Source: Internet
Author: User
Tags constant constructor inheritance
Inheritance 3, restrictions on the use of accessibility levels in inheritance

When declaring a type, it is most important to see whether the type must have at least the same accessibility as other members or types. For example, a direct base class must have at least the same accessibility as a derived class. The following declaration will cause a compiler error because the baseclass of the base class is less than MyClass:


Class BaseClass {...}
public class Myclass:baseclass {...}//Error

The following table summarizes the restrictions on the accessibility level used for declarations.


Context Notes
The direct base class of a class class type must be at least as accessible as the class type itself.
The explicit base interface of an interface type must be at least as accessible as the interface type itself.
The return type and parameter type of the delegate delegate type must be at least as accessible as the delegate type itself.
The type of a constant constant must be at least as accessible as the constant itself.
The type of the field field must have the same accessibility as at least the field itself.
The return type and parameter type of a method method must be at least as accessible as the method itself.
The type of the property property must be at least as accessible as the property itself.
The type of an event event must be at least as accessible as the event itself.
The type and parameter types of the indexer indexer must be at least as accessible as the indexer itself.
The return type and parameter type of the operator operator must be at least as accessible as the operator itself.
The parameter types of a constructor constructor must be at least as accessible as the constructor itself.

Example: The following example contains different types of error declarations. The comment after each declaration indicates the expected compiler error.


Using System;
delegate int mydelegate ();
Class B
{//define a private function:
static int Myprivatemethod ()
{return 0;}
}
public class A
{//Field definition:
Public B MyField = new B ();//error: Type B differs from a field A.myfield level
constructor function:
Public readonly B myconst = new B (); Error: Type B is read-only
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");
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.