Class basetest {public int A = 10; protected int B = 2; protected void test () {}} class childtest: basetest {int C; int D; Public void printtest () {// The protected keyword is a member access modifier. A protected member can be accessed by a derived class instance in its class. // The protected member of the base class is only accessible in the derived class when access is made through the derived class. // => (1) access the console through base. writeline (base. a. tostring (); console. writeline (base. b. tostring (); // => (2) basetest = new basetest (); // equivalent to basetest basetest1 = new childtest (); console. writeline (basetest. a); // console. writeline (basetest. b); // inaccessible // => (3) include the class derived type childtest = new childtest (); console. writeline (childtest. a. tostring (); console. writeline (childtest. b. tostring ());}}
C # basic knowledge protected keywords