Base (C # Reference)

Source: Internet
Author: User

Original address: Https://msdn.microsoft.com/zh-cn/library/hfw7t1ce.aspx

The base keyword is used to access the members of the base class from a derived class:

    • Calls a method on the base class that has been overridden by another method.

    • Specifies the base class constructor that should be called when creating an instance of a derived class.

Base class access can only be done in constructors, instance methods, or instance property accessors.

It is wrong to use the base keyword from a static method (where the condition estimates are consistent with the This keyword, because static methods are at the class level and not at the object level).

The base class that is accessed is the base class specified in the class declaration. For example, if you specify class Classb:classa, the members of ClassA are accessed from ClassB regardless of the base class of ClassA.

Example

In this case, both the base class person and the derived class Employee have a method named Getinfo. you can call the base class's Getinfo method from a derived class by using the base keyword .

C#
 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 (); }}/*outputname:john L. Malgrainessn:444-55-6666employee id:abc567efg*/

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

Example

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

C#
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 would call Baseclass.baseclass () public    DerivedClass  (): Base ()    {    }    //This constructor would call Baseclass.baseclass (int. i) public    derivedclass (int i): Base (i)    {    }    static void Main ()    {        DerivedClass md = new DerivedClass ();        DerivedClass md1 = new DerivedClass (1);}    } /*output:in BaseClass () in baseclass (int i) */

Base (C # Reference)

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.