C # keyword this

Source: Internet
Author: User

The keyword "this" has two basic usage methods:
First, it is used for this access.
Second, specify the constructor to be executed when declaring the constructor.
This access
In the instance constructor and instance function member of the class, the keyword "this" indicates the reference of the current class instance or object. This cannot be used in static constructor and static function members, or in other places.
When you use the same variable name or parameter name as the field name in the instance constructor or method, you can use this to distinguish between fields and variables or parameters. The following code demonstrates the usage of this.
Public class dog
{
Public string name;
Public int age;
Public dog ()
{
}
Public dog (string name) // in this function, name refers to the input parameter name
{
This. Name = Name; // This. name indicates the field name.
}
Public dog (string name, int age) // in this function, name refers to the input parameter name
{// Age refers to the input parameter age
This. Name = Name; // This. name indicates the field name.
This. Age = age; // This. Age indicates the field age.
}
}
In fact, this is defined as a constant. Therefore, although in the instance constructor of the class and the instance function Member, this can be used to reference the instance involved in the call of the function member, however, you cannot assign values to or change the value of this. For example, operations such as this ++ and -- this are invalid.
 
This is used to declare the constructor.
You can declare the instance constructor in the following format:
"Access modifier" [class name] ("form parameter table"): This ("actual parameter table 』)
{
[Statement block]
}
This indicates another instance constructor that declares the class and matches the form parameter table most with the actual parameter table. This constructor will be executed before the declared constructor is executed.
For example:
// Thisandconstructor. CS
// Keyword this is used to declare the constructor
Using system;
Class
{
Public A (int n)
{
Console. writeline ("A. A (INT n )");
}
Public A (string S, int N): This (0)
{
Console. writeline ("A. A (string S, int n )");
}
}
Class Test
{
Static void main ()
{
A A = new A ("a class", 1 );
}
}

Output:
A. A (int n)
A. A (string S, int N)
This indicates that the constructor A (int n) is executed before the constructor A (string S, int N) is executed ).

 

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.