Detailed description of protected usage in C #

Source: Internet
Author: User
(From http://blog.csdn.net/lijinlin/article/details/5954688)

  In the Accessibility level of C #, public and private are the easiest to understand. On the contrary, protected is often a headache. Friends who often Query Information on the Internet often find that,
The answer is almost always the same as the previous one, not copied from msdn or copied from others' blogs. Article Let's take a look at the differences between us and others.
What is the protected access modifier.


Next, let's take a look at the definition of the protected access modifier in most documents: access is limited to include classes or types derived from include classes. It is impossible to understand the problem only by looking at it. practice is required.
We use facts to illustrate the problem.

CodeAs follows:

Class basetest

{

Public int A = 10;

Protected int B = 2;

}

Class childtest: basetest

{

Int C;

Int D;

Public void printtest ()

{

Basetest = new basetest ();

This. A = basetest.;

This. c = basetest. B;

}

}


The code in the last line of the method in the derived class in this code will report an error: the member cannot be accessed through the "protectedtest. Vehicle" qualifier.
"Protectedtest. vehicle. Weight"; the qualifier must be of the type "protectedtest. Car" (or derived from this type ). See this
There will be a lot of people to ask, isn't the protected type access limited to include classes or derived from include classes? Why is an error reported here? Let's explain it below, include class energy
If the protected type is enough, there should be no problem. I don't need to talk about it here. The main problem is why the basic instances in the above example cannot be accessed.
The variables of the protected type are involved in understanding the protected type. The protected type is not accessible by the derived class but can be accessed at will.
If conditions exist, the access must be based on the type of the derived class. Only the members of the protected type of the base class in the derived class can be accessed, it is clear that the above Code is not based on the class of the derived class.
Type access, but access through the base class type. At this moment, protected and private are very similar, except that the protected type member can be inherited by the derived class.
And can be accessed through the derived class type.

It is correct to change it to the following form. However, we are stuck in the two code forms and want to see what the problem is.

Class basetest

{

Public int A = 10;

Protected int B = 2;

}

Class childtest: basetest

{

Int C;

Int D;

Static void main (string [] ARGs)

{

Basetest = new basetest ();

Childtest = new childtest ();

Console. writeline (childtest. B );

}

}

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.