C # base keyword,

Source: Internet
Author: User

C # base keyword,

1. The base keyword is used to access the members of the base class from the derived class.

2. Call methods that have been overwritten by other methods on the base class.

3. Specify that the instance for creating a derived class should call the constructor of the base class.

4. Public and protected members accessing the base class cannot access private members.

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

In the following example, both the base class Person and the derived class Employee have a GetInfo () method. The base keyword can be used to call the GetInfo () method of the base class from the derived class.

Class TestBase {static void Main () {Employye employee = new Employye (); employee. getInfo (); Console. read () ;}} public class Person {public string Name = "jing"; public int Age = 20; public virtual void GetInfo () {Console. writeLine ("Name: {0}, Age: {1}", Name, Age) ;}} public class Employye: Person {public string Id = "123456 "; public override void GetInfo () {base. getInfo (); Console. writeLine ("employee Id: {0}", Id );}}

 

Related Article

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.