C uses the this keyword to implement a concatenated constructor call method

  • 2020-12-26 05:51:50
  • OfStack

This article is an example of how C# uses the this keyword to implement a concatenated constructor call method. Share to everybody for everybody reference. The specific analysis is as follows:

In a class if you need to implement multiple custom constructor, it is usually in the constructor to realize their business logic, if the business logic implementation is not distinct, obviously do not conform to the oop programming ideas, hardly conducive to maintenance, of course, we also can by the same logic part of the encapsulated into a method, but there is one kind of more reasonable and simple method, the following is to achieve serial constructor by this keywords to do a simple example.

The sample code is as follows:


public class Person
{       
        public string personName;
        // Define age as a nullable type , So you can give it null value
        public int? personAge;         // Under the front 3 Each constructor has the most arguments to call 4 Three constructors , Just take some of the parameters they need
        // So this is the way to do it this Series constructor
        public Person():this("",0)
        {
           
        }         public Person(string name):this("evan",null)
        {
           
        }         public Person(int age):this("",20)
        {
           
        }         public Person(string name, int? age)
        {
            this.personName = name;
            // through ?? Judge incoming age Whether or not null value
            // If you belong to null value , The assignment 100
            this.personAge = age ?? 100;
        }         public void Display()
        {
            Console.WriteLine("Name:{0},Age:{1}\n", personName, personAge);
        }       
}

The main function call is as follows:


static void Main(string[] args)
{
        Person per1 = new Person();
        per1.Display();                    Person per2 = new Person(20);
        per2.Display();                    Person per3 = new Person("evan");
        per3.Display();                    Person per4 = new Person("evan", 20);
        per4.Display();         Console.ReadLine();           
}

This approach is to make one accept parameters most constructor do "the primary constructor", and is realized in the primary constructor must business logic, the rest of the constructor as long as you use this keyword put forward incoming parameters to the primary constructor, and provide necessary of other parameters, like this, our whole class we need to worry about is that the primary constructor, the rest of the constructor basically can be null.

Note: If the constructor chain still has the logic to implement the respective logic, then the code for the primary constructor is executed first, and the respective logic is executed later. By doing so, the real work is left to one constructor, and the class definition is cleaner, more maintainable, and simplifies the programming task.

Hopefully this article has helped you with your C# programming.


Related articles: