1. Keyword this
① refers to the current object's own
When you want to explicitly indicate the use of the object's own variable or function in a class, you should add the This keyword, and the small chestnut A is as follows:
Public classA {stringName ="I am a number ~ ~"; PublicAstringName) {Console.WriteLine ("When this is not added, the value of name is: {0}", Name);//at this point the output is the current function's own variableConsole.WriteLine ("does not assign a parameter to a member variable name value, this. The value of name is: {0}", This. Name);//the output is now a member variable of the current class This. Name =Name; Console.WriteLine ("A parameter that is assigned to a member variable after the name value, this. The value of name is: {0}", This. Name);//the assignment was made at this time } } Static voidMain (string[] args) {Console.WriteLine (); A A=NewA"I'm number second ~ ~"); Console.readkey (); }
Output Result:
② passing this as a parameter
When you want to pass your arguments to another object, the This keyword is used, and the little chestnut B is as follows:
Public classA {b b; Public voidShow () {b=NewB This); Console.WriteLine ("This represents the object of a that will be created"); } } Public classB {A A1; PublicB (A A2)//That is, the instance of B has a member variable of type a{A1=A2; } } Static voidMain (string[] args) {A A=NewA (); A.show (); Console.readkey (); }
③ in the constructor, through this can call the same class inside the other constructors, also known as the this concatenation constructor, the small chestnut C is as follows:
Public classPerson { Public stringPersonName; //defines an age as a nullable type so that it can be given a null value Public int?personage; //The first three constructors below call the fourth constructor with the most parameters, and take only the partial arguments they need//This is the this concatenation constructor PublicPerson (): This("",0) { } PublicPerson (stringName): This("Evan",NULL) { } PublicPerson (intAge): This("", -) { } PublicPerson (stringNameint?Age ) { This. PersonName =name; //determine if the passed-in age is a null value, and if it is a null value, assign a value of This. personage = age?? -; } Public voidDisplay () {Console.WriteLine ("Name:{0},age:{1}", PersonName, personage); } }
Static voidMain (string[] args) {Person Per1=NewPerson (); Per1. Display (); Person Per2=NewPerson ( -); Per2. Display (); Person Per3=NewPerson ("Evan"); Per3. Display (); Person Per4=NewPerson ("Evan", -); Per4. Display (); Console.ReadLine (); }
Operation Result:
http://i.cnblogs.com/EditPosts.aspx?postid=5019381
This is done by having a constructor that takes the most parameters as the "main constructor" and implementing the necessary business logic in the main constructor, as long as the rest of the constructors use the This keyword to forward the passed parameters to the main constructor, and provide the necessary other parameters, so that our entire class What we need to worry about is the main constructor, and the rest of the constructors are basically empty.
Note: If the constructor chain also implements its own logic, then it is actually the code that executes the main constructor, and then the respective logic, and with this practice, the real work is given to a constructor, and the class definition is simpler, easier to maintain, and simplifies programming tasks.
④this can be used to declare indexers
1. First, what is an index?
An index is also a class member and must be an instance member because it is a way of accessing an instance member, so it cannot be declared static. Indexes are similar to properties and have get accessors and set accessors. And the index is actually a set of get accessors and set accessors, and the nature of the accessor is the method, so the essence of the indexer is the method. indexers are often declared in types that are primarily used to encapsulate internal collections or arrays.
2. The index declaration uses the following syntax:
[Access modifier] return value type this [parameter 1, Parameter 2 ...]
{
Get{...}
Set{...}
}
Syntax points: The index name is always this; The index parameter is in the middle of the square brackets; The index has at least one parameter in the parameter list.
Small chestnuts d are as follows:
Public classDemo { Public stringName {Get;Set; } Private string[] Sex =New string[3]; Public string This[intIndex] { Get { if(index<3) { returnSex[index]; } return NULL; } Set { if(index<3) {Sex[index]=value; } } } } Static voidMain (string[] args) {Demo Demo=NewDemo (); demo[0] ="female"; demo[1] ="male"; demo[2] ="other"; Demo. Name="Xiao Ming"; Console.WriteLine ("The three instances of name {3},sex are: {0},{1},{2}.", demo[0], demo[1],demo[2],demo. Name); Console.readkey (); }
Operation Result:
3. An indexer allows an instance of a class or struct to be indexed in the same way as an array, with an indexer similar to an attribute, unlike an indexer whose access is a parameter.
Indexer and Array Comparisons:
(1) The indexer has an unrestricted index value (index) type
(2) Indexers allow overloading
(3) Indexer is not a variable
Different points of indexers and attributes
(1) Attributes are identified by name, and indexers are identified as functions
(2) indexers can be overloaded and properties cannot be
(3) The indexer cannot be declared static, and the property can
The role of the keyword this