namespacethisorbase{//Reference Address:http://blog.sina.com.cn/s/blog_7300c7d90100rs20.html /*At this time, the _str of the derived class and the base class are differentiated, * The derived class defines a self-_str (note that when defining, * Please add the new keyword, although it can be compiled through, * but recommended), so that this real value is embodied. * This, which points to the self-defined field of the derived class object. * Base is the field that points to the base class. This is the true meaning of this and base in C#.net. */ Public classFather {protected string_str ="the initial value of the Father object string"; PublicFather () { This. _str ="Father Object constructor This variable string initial value"; } } Public classSon:father {Private New string_str ="Child Class object string initial value"; PublicSon () {Base. _str ="Subclass Object constructor Modifies parent class initialization"; } Public stringPrintstr_this () {return This. _str; } Public stringPrintstr () {return_str; } Public stringprintstr_base () {return Base. _str; } } classProgram {Static voidMain (string[] args) {Son MyObj=NewSon (); Console.WriteLine (myobj. Printstr_this ()); Console.WriteLine (myobj. Printstr ()); Console.WriteLine (myobj. Printstr_base ()); Console.read (); } }}
C # Use of this and base