The example in this article describes C # using the This keyword to implement a concatenation constructor call method. Share to everyone for your reference. The specific analysis is as follows:
In a class, if you need to implement multiple custom constructors, it is common practice to implement their own business logic in the constructor, if the implementation of these business logic is not entirely different, obviously does not conform to OOP programming ideas, is very detrimental to maintenance, of course, we can also by the same logical part of the package into a method, But there is a more reasonable and simple way to do this by implementing a concatenation constructor using the This keyword as a simple example.
The sample code is as follows:
The code is as follows:
public class Person
{
public string personname;
Defines an age as a nullable type so that it can be given a null value
public int? personage;
The first three constructors below call the fourth constructor with the most parameters, and take only the partial arguments they need
This is the this concatenation constructor
Public person (): This ("", 0)
{
}
Public person (string name): This ("Evan", NULL)
{
}
Public person (INT.): This ("", 20)
{
}
Public person (string name, Int. age)
{
This.personname = name;
Pass?? Determines whether the passed-in age is a null value
If it is a null value, it is assigned a value of 100
This.personage = age?? 100;
}
public void Display ()
{
Console.WriteLine ("Name:{0},age:{1}\n", PersonName, personage);
}
}
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # using the This keyword to implement a concatenation constructor call method
This address: http://www.paobuke.com/develop/c-develop/pbk23157.html
Related content attribute and reflection of C # Basic Learning Series C # time operation class share C # use Npoi upload excelc# do not log on to the computer launcher program
Dynamically set file permissions with C # code Wpf?¢d?á?ììoíí¨???? °′?¥?ùê?′ú???? How to use WinForm to implement a cool transparent animation interface in Íc# C # method of using JavaScript to realize the countdown of second kill
C # using the This keyword to implement a concatenation constructor call method