Keywords in C #: Base, this (1)

Source: Internet
Author: User

C #KeywordsBase and this. The base keyword is used to access a base class member from a derived class: it calls a method that has been overwritten by other methods on the base class.

Specify the base class constructor to call when creating a derived class instance.

Base Class access can only be performed in constructors, instance methods, or instance attribute accessors.

  Example:

Call the base class method in the derived class.

The following is a reference snippet: <br/> // base keyword <br/> // access base class member <br/> using system; <br/> public class baseclass <br/> {<br/> protected string _ classname = "baseclass"; <br/> Public Virtual void printname () <br/> {<br/> console. writeline ("Class Name: {0}", _ classname); <br/>}< br/> class derivedclass: baseclass <br/>{< br/> Public String _ classname = "derivedclass"; <br/> Public override void printname () <br/>{< br/> console. write ("The baseclass name is {0}"); <br/> // call the base class method <br/> base. printname (); <br/> console. writeline ("This derivedclass is {0}", _ classname ); <br/>}< br/> class testapp <br/>{< br/> Public static void main () <br/>{< br/> derivedclass Dc = new derivedclass (); <br/> DC. printname (); <br/>}< br/> /**//*

Console output:

The baseclass name is baseclass

This derivedclass is derivedclass

*/

Call the base class constructor In the derived class.

<Br/> // keywords_base2.cs <br/> using system; <br/> public class baseclass <br/>{< br/> int num; <br/> Public baseclass () <br/> {<br/> console. writeline ("in baseclass ()"); <br/>}< br/> Public baseclass (int I) <br/>{< br/> num = I; <br/> console. writeline ("in baseclass (INT {0})", num); <br/>}< br/> public class derivedclass: baseclass <br/>{< br/> // The constructor calls baseclass. baseclass () <br/> Public derivedclass (): Base () <br/>{< br/>}< br/> // The constructor calls baseclass. baseclass (int I) <br/> Public derivedclass (int I): Base (I) <br/>{< br/>}< br/> static void main () <br/>{< br/> derivedclass Dc = new derivedclass (); <br/> derivedclass DC1 = new derivedclass (1 ); <br/>}< br/> /**//*

Console output:

In baseclass ()

In baseclass (1)

*/

  Note:

It is incorrect to use the base keyword in a static method.

  BaseIt is mainly used for the polymorphism of object-oriented development, which is embodied in example 2.

  ThisKeyword to reference the current instance of the class.

The blog address of the link is: http://blog.163.com/huang_ying_lu/blog/static/269998320081109212363/

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.