CSharp keyword-base

Source: Internet
Author: User

BaseKeyword is used to access a member of the base class from a derived class:

  • Call methods that have 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.

Use from static methodsBaseKeyword is incorrect.

Example

In this example, the base classPersonAnd Derived classesEmployeeEach has a nameGetinfo. UseBaseKeyword. You can callGetinfoMethod.

// keywords_base.cs// Accessing base class membersusing System;public class Person{    protected string ssn = "444-55-6666";    protected string name = "John L. Malgraine";    public virtual void GetInfo()    {        Console.WriteLine("Name: {0}", name);        Console.WriteLine("SSN: {0}", ssn);    }}class Employee : Person{    public string id = "ABC567EFG";    public override void GetInfo()    {        // Calling the base class GetInfo method:        base.GetInfo();        Console.WriteLine("Employee ID: {0}", id);    }}class TestClass{    static void Main()    {        Employee E = new Employee();        E.GetInfo();    }}

This example shows how to specify the base class constructor called when creating a derived class instance.

// keywords_base2.csusing System;public class BaseClass{    int num;    public BaseClass()    {        Console.WriteLine("in BaseClass()");    }    public BaseClass(int i)    {        num = i;        Console.WriteLine("in BaseClass(int i)");    }    public int GetNum()    {        return num;    }}public class DerivedClass : BaseClass{    // This constructor will call BaseClass.BaseClass()    public DerivedClass()        : base()    {    }    // This constructor will call BaseClass.BaseClass(int i)    public DerivedClass(int i)        : base(i)    {    }    static void Main()    {        DerivedClass md = new DerivedClass();        DerivedClass md1 = new DerivedClass(1);    }}

Output

Name: John L. MalgraineSSN: 444-55-6666Employee ID: ABC567EFG

For other examples, see new, virtual, and override.

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.