Derived classes in C # Call base class constructor usage analysis

Source: Internet
Author: User

The default constructor here refers to the system default parameterless constructor without writing the constructor

1. When a constructor is not written by itself in the base class, the default constructor of the base class is called by the derived class
For example:

?
123456789101112131415161718 public class MyBaseClass{}public class MyDerivedClass : MyBaseClass{  public MyDerivedClass()  {   Console.WriteLine("我是子类无参构造函数");  }  public MyDerivedClass(int i)  {   Console.WriteLine("我是子类带一个参数的构造函数");  }  public MyDerivedClass(int i, int j)  {   Console.WriteLine("我是子类带二个参数的构造函数");  }}

When instantiating a derived class, call the base class default constructor

2. When a constructor is written in a base class, the derived class does not specify which constructor the call constructs, it looks for the parameterless constructor, and if none is an error, and no matter which constructor in the derived class is called, it is the base class constructor that looks for the parameterless, not the parameter match.
For example:

?
12345678910111213141516171819202122 public class MyBaseClass{  public MyBaseClass(int i)  {   Console.WriteLine("我是基类带一个参数的构造函数");  } } public class MyDerivedClass : MyBaseClass {  public MyDerivedClass()  {   Console.WriteLine("我是子类无参构造函数");  }  public MyDerivedClass(int i)  {   Console.WriteLine("我是子类带一个参数的构造函数");  }  public MyDerivedClass(int i, int j)  {   Console.WriteLine("我是子类带二个参数的构造函数");  }}

Error when instantiating a derived class

3. A constructor is written in a base class, and a derived class can specify a constructor that calls the base class, using the base keyword.
For example

?
12345678910111213141516171819202122 public class MyBaseClass{ public MyBaseClass(int i) {  Console.WriteLine("我是基类带一个参数的构造函数"); }}public class MyDerivedClass : MyBaseClass{ public MyDerivedClass() : base(i) {  Console.WriteLine("我是子类无参构造函数"); } public MyDerivedClass(int i) : base(i) {  Console.WriteLine("我是子类带一个参数的构造函数"); } public MyDerivedClass(int i, int j) : base(i) {  Console.WriteLine("我是子类带二个参数的构造函数"); }}

When you instantiate a constructor with a parameter that is used when deriving a class, you do not get an error because he specifies the constructor of the base class.

4, if the constructor in the base class does not contain a parameterless constructor, the constructors in the derived class must all specify the base class constructor for the call, or else an error
For example

?
12345678910111213141516171819202122 public class MyBaseClass {  public MyBaseClass(int i)  {   Console.WriteLine("我是基类带一个参数的构造函数");  } } public class MyDerivedClass : MyBaseClass {  public MyDerivedClass()  {   Console.WriteLine("我是子类无参构造函数");  }  public MyDerivedClass(int i) : base(i)  {   Console.WriteLine("我是子类带一个参数的构造函数");  }  public MyDerivedClass(int i, int j)  {   Console.WriteLine("我是子类带二个参数的构造函数");  }}

At this point the compilation will not pass

I hope this article is helpful to everyone's C # programming.

Derived classes in C # Call base class constructor usage analysis

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.