The role of the keyword base

Source: Internet
Author: User

① calls a method on the base class that has been overridden by another method, and the small chestnut A is as follows:

 Public classFather { Public Virtual voidShow () {Console.WriteLine ("I am Your Father"); }        }         Public classChild:father { Public Override voidShow () {Base.                Show (); Console.WriteLine ("I am your child"); }        }

Operation Result:

② when creating an instance of a derived class, specify the constructor of the base class that is called

        Public classFather { Public stringName {Get;Set; }  PublicFather (stringName) {                 This. Name =Name; }        }         Public classChild:father { PublicChild ():Base("Xiao Ming") {Console.WriteLine ("output base class name:{0}", Name); }        }        Static voidMain (string[] args) { Child child=NewChild ();        Console.readkey (); }

Operation Result:

When a subclass is constructed, the constructor of the parent class is called first, but you can choose which constructor to call with the base keyword, but not each (at least one). If you do not specify it, you typically call the parameterless constructor, because this is the default constructor for a class.

The role of the keyword base

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.