This keyword is used to call the series constructor, and the key Constructor

Source: Internet
Author: User

This keyword is used to call the series constructor, and the key Constructor

If you need to implement multiple custom constructors in a class, the common practice is to implement their own business logic in the constructor. If the implementation of these business logic is not completely different, obviously, it is not in line with the idea of oop and is very difficult to maintain. Of course, we can also encapsulate the same logic part into a method, but there is a more reasonable and simple method, the following is a simple example of concatenating constructors using the this keyword.

The sample code is as follows:

1 public class Person 2 {3 public string personName; 4 // defines the age as an empty type, so that the null value of 5 public int can be assigned to it? PersonAge; 6 7 // The first three constructors are the fourth constructors that call the most parameters, take only some of the required parameters. // this method is used to concatenate the constructor 9 public Person (): this ("", 0) 10 {11 12} 13 14 public Person (string name): this ("evan", null) 15 {16 17} 18 19 public Person (int age ): this ("", 20) 20 {21 22} 23 24 public Person (string name, int? Age) 25 {26 this. personName = name; 27 // pass ?? Determine if the input age is null 28 // if it belongs to null, the value is 10029 this. personAge = age ?? 100; 30} 31 32 public void Display () 33 {34 Console. writeLine ("Name: {0}, Age: {1} \ n", personName, personAge); 35} 36}

The main function is called as follows:

 1 static void Main(string[] args) 2     { 3         Person per1 = new Person(); 4         per1.Display();             5  6         Person per2 = new Person(20); 7         per2.Display();             8  9         Person per3 = new Person("evan");10         per3.Display();            11 12         Person per4 = new Person("evan", 20);13         per4.Display();14 15         Console.ReadLine();            16      }

This approach is to make a constructor that accepts the most Parameters "Main constructor" and implement the necessary business logic in the main constructor, the other constructors only need to use the this keyword to forward the input parameters to the main constructor and provide other required parameters. In this way, what we need to worry about in the entire class is the main constructor, and the rest of the constructor can basically be empty.

(Note: If the constructor chain still implements its own logic, it is actually to first execute the code of the main constructor and then execute the respective logic). This approach is used, the real work is handed over to a constructor, and the class definition is simpler, easier to maintain, and easier to program tasks.

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.