C # Basic knowledge of this keyword introduction _c# tutorial

Source: Internet
Author: User

A, this can represent the current instance of a reference class, including inherited methods, which can usually be omitted.

Copy Code code as follows:

public class Person
{
public string Name {get; set;}
public int Age {get; set;}
Public person (string Name, int age)
{
This. Age = age;
This. name = name;
}
}

This is needless to say, when an object calls its own internal function, the object is used to use this.

Second, this keyword followed by the ":" Symbol, you can invoke other constructors

Copy Code code as follows:

Declaring constructors that have implementations
Public person ()
{
This. nage = 100;
Console.WriteLine ("I am Superman!") ");
}
public person (int nage)
{
Console.WriteLine ("Superman's age {0}", nage);
}
The constructor of a second parameter was called using the This keyword
public person (int nage, string strName)
: This (1)
{
Console.WriteLine ("I am called Superman of {0}, age {1}", StrName, nage);
}

We create the object to see if the call succeeds. Add the following code to the main function:

Copy Code code as follows:
Person p = new person (10, hadron);

The execution will output:

Copy Code code as follows:

Superman's Age 1
I'm Superman, age 10.

Third, declaring indexers

An indexer type indicates which type of index the indexer uses to access an array or collection element, can be an integer, can be a string; This represents an array or collection member manipulating this object, which can be easily understood as the name of the indexer, so the indexer cannot have a user-defined name. For example:

Copy Code code as follows:

public class Person
{
string[] personlist = new STRING[10];
public string this[int param]
{
get {return personlist[param];}
set {Personlist[param] = value;}
}
}

The data type of the index must be the same as the index type of the indexer. For example:

Copy Code code as follows:

person who = new person ();
Person[0] = "Hello";
PERSON[1] = "World";
Console.WriteLine (Person[0]);

The object looks like a number of groups, hehe.

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.