C # application of the object-oriented this keyword

Source: Internet
Author: User

This keyword refers to the current object itself, and generally refers to the object itself

In a class, the constructor usually calls one party with fewer parameters.

The following is an example:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace ooptext
{
Public class person
{
/// <Summary>
/// Field
/// </Summary>
Private int ID;

Public int ID
{
Get {return ID ;}
Set {id = value ;}
}
Private string name;

Public string name
{
Get {return name ;}
Set {name = value ;}
}
Private string password;

Public String Password
{
Get {return password ;}
Set {Password = value ;}
}

/////////// If two parameters are sent, I also call the constructor of the three parameters in disguise, but I write the third parameter as a constant.
Public Person (int id, string name)
: This (ID, name, "456789 ")
{

}

/////////// When three parameters are sent, directly drop the constructor with three parameters. This refers to the object itself that is currently instantiated.
Public Person (int id, string name, string password)

{
This. ID = ID;
This. Name = Name;
This. Password = password;
}
Public void say ()
{
Console. writeline ("Hello {0} My name is {1}, my password is {2}", ID, name, password );
}
Static void main ()
{
Person = new person (1, "Xiaogang ");
Person. Say ();
}
}

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.