": This"

Source: Internet
Author: User

Recently I found this very interesting. When we need multiple constructors, We can greatly reduce the amount of code ~~ First look at a simple demo

Class person
{
Public string name {Get; set ;}
Public int age {Get; set ;}
Public Person (string names, int iage)
{
This. Name = names;
This. Age = iage;
}
Public Person (): This ("trigged", 21 ){}
Public Person (string names): This (names, iage: default (INT )){}
Public Person (INT iage): This ("", iage ){}
Public override string tostring ()
{
Return string. Format ("name is {0}, age is: {1}", name, age );
}
}

// Call the following code:

Person p1 = new person ();
Console. writeline (P1 );
Person P2 = new person (20 );
Console. writeline (P2 );
Person P3 = new person ("trigged ");
Console. writeline (P3 );
Person P4 = new person ("trigged", 21 );
Console. writeline (P4 );
Console. readkey ();

The code is very simple, but the red part uses: This. Note that the less ":" meaning is changed. To put it simply, multiple constructors can share the same implementation.

In the code, we define four constructor, but only the first constructor implements the value assignment, and the other three do not have the specific value assignment. This is the call result

~ If you have already guessed it, you can end this article. If you find it interesting, let's analyze it together ~

When you use reflector to open the file, do not rush to check il. You can set the version to none first.

PublicPerson()

{

This .. ctor ("trigged", 21 );

Return;

}

PublicPerson(INT iage)

{

This .. ctor ("", iage );

Return;

}

PublicPerson(String names)

{

IntNum;

Num = 0;

This .. ctor (names, num );

Return;

}

PublicPerson(String names, int iage)

{

Base .. ctor ();

This. Name = names;

This. Age = iage; return;

}

From this we can find that the first three constructors call the final constructor through: this. If you do not believe it, you can add an output statement to the Fourth constructor.

Public Person (string names, int iage)
{
Console. writeline ("I 'was constructed ");
This. Name = names;
This. Age = iage;
}

Other code remains unchanged, and this statement is output during instantiation.

 

Enjoy ~

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.