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.