C # using the This keyword to implement a concatenation constructor call method

Source: Internet
Author: User

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

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.