Home > Article > Backend Development > C# Learning Diary 28---Indexer, overloaded indexer
Indexers allow instances of classes or structures to be indexed just like an array. When you define an indexer for a class, the class will behave like an array. You can use the array accessor '[]' for this The instance of the class is accessed. The indexer is similar to the attribute, but the indexer takes parameters. (You can also think that the indexer is an attribute of a class, which is also implemented through an accessor (a special method))
To make it clearer, let’s give an example. College is the most leisurely time in life. Students can do many things that they once wanted to do but did not do for various reasons. Skipping classes is obviously one of them. 1. In order to stop the behavior of skipping classes, every time in class, the teacher will call the name or the student number. I am in Class 4. The teacher likes to call the student number, "Class 4 No. 1" and then answer loudly "To..." ", "Class 4, No. 2", "Class 4, No. 3"... "No. 3. Classmate.", "Teacher HC666 has a stomachache and went to the hospital today." (Good morning, China. Roommate ^_^), "Oh, No. 4"... The teacher's roll call is an index to Class 4.
If class4 is regarded as an object instantiated by the Classes class, class[1] is the index of classmate No. 1, (think of the array and you will understand) then how to implement it?
Define indexer:
As mentioned above, indexers are similar to properties. Naturally, get and set accessors are also indispensable. Indexers are also members of the class, so naturally they must be in the class. It is defined as follows:
public 返回值类型 this[参数类型 参数] { get { return 参数指定的值; } //get访问器 set { /*设置参数指定的值 */ } //set访问器 }
Instance of indexer:
We will code the above example as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test1 {//定义一个能被索引的类 class Classes {//对类的索引实质是对类中数组的索引 public string[] StudentName=new string[6]; //定义索引器 public string this[int ID] { get { return StudentName[ID]; } set { StudentName[ID] = value; } //set访问器自带value参数 } } class Program { static void Main(string[] args) { Classes class4 = new Classes(); //索引写入 for (int i = 1; i < 6; i++) { class4[i] = "HC"+i; } //索引读出 for (int j = 1; j < 6; j++) { Console.WriteLine(j+"号\t"+class4[j]); } } } }
Result:
Overloaded indexer:
In the above program, we have indexed the student’s name through the student number, so how can we index the student number through the name? Woolen cloth? We regard the index as a special method. The method can use overloading to achieve the same function with different parameters, so the indexer can naturally be overloaded. The usage is similar to method overloading (click here to view method overloading). We will continue the above example. This time we need to use the name index to get the student number:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test1 {//定义一个能被索引的类 class Classes {//对类的索引实质是对类中数组的索引 public string[] StudentName=new string[6]; //定义索引器 public string this[int ID] { get { return StudentName[ID]; } set { StudentName[ID] = value; } //set访问器自带value参数 } //重载索引器参数设为string类型 public uint this[string name] { get { //找到与name匹配的学号 uint index=1; while (StudentName[index] != name && index < 6) { index++; } return index; } set { } } } class Program { static void Main(string[] args) { Classes class4 = new Classes(); //索引写入 for (int i = 1; i < 6; i++) { class4[i] = "HC"+i; } //索引读出,通过学号索引出姓名 for (int j = 1; j < 6; j++) { Console.WriteLine(j+"号\t"+class4[j]); } //通过姓名索引出学号 for (int k = 1; k < 6; k++) { string name="HC"+k; Console.WriteLine(name+"\t"+class4[name]+"号");//对比上面用法一样参数不一样 } } } }
Result:
We said above that when a class defines an indexer, the class can be treated as an array. When learning arrays, we know that arrays have multiple dimensions, and the indexer is located What about classes? ? Can the foreach traversal statement we use to traverse the array also be used for this class? ? I’ll introduce it in the next article! ! (Because the school is going to have a power outage...) I hope you will continue to support HC666^_^
The above is the content of C# Learning Diary 28---indexer and overloaded indexer. For more related content, please pay attention to PHP Chinese website (www.php.cn)!