C # Indexer (Indexer)
An indexer (Indexer) allows an object to be indexed as an array. When you define an indexer for a class, the class behaves as if it were a virtual array. You can use the array access operator ([]) to access an instance of the class.
Grammar
The syntax for a one-dimensional indexer is as follows:
Element-type This[int index] { //get accessor get { //return index specified value } //Set accessor set { Sets the value specified by index }}
Purpose of the indexer (Indexer)
The declaration of the behavior of the indexer is somewhat similar to the property. Like property, you can use get and set accessors to define indexers. However, the property returns or sets a specific data member, and the indexer Returns or sets a specific value for the object instance. In other words, it divides the instance data into smaller parts and indexes each part to get or set each part.
Defines a property that includes providing a property name. The indexer is defined without a name, but with the This keyword, which points to an object instance. The following example demonstrates this concept:
Using System;namespace indexerapplication{class Indexednames {private string[] NameList = new String[size]; static public int size = 10; Public Indexednames () {for (int i = 0; i < size; i++) namelist[i] = "N. A."; } public string This[int index] {get {string tmp; if (index >= 0 && index <= size-1) {tmp = Namelist[index]; } else {tmp = ""; } return (TMP); } set {if (index >= 0 && index <= size-1) {namelist[i Ndex] = value; }}}} static void Main (string[] args) {indexednames names = new Indexednames (); Names[0] = "Zara"; NAMES[1] = "Riz"; NAMES[2] = "Nuha"; NAMES[3] = "Asif"; NAMES[4] = "Davinder"; NAMES[5] = "SunIl "; NAMES[6] = "Rubic"; for (int i = 0; i < indexednames.size; i++) {Console.WriteLine (names[i]); } console.readkey (); } }}
When the above code is compiled and executed, it produces the following results:
Zarariznuhaasifdavindersunilrubicn. A.N. a.n. A.
Overloaded indexer (Indexer)
Indexers (Indexer) can be overloaded. Indexers can also have multiple parameters when declared, and each parameter can be of a different type. It is not necessary to have an indexer that is an integral type. C # allows indexers to be other types, for example, string types.
The following example shows an overloaded indexer:
Using System;namespace indexerapplication{class Indexednames {private string[] NameList = new String[size]; static public int size = 10; Public Indexednames () {for (int i = 0; i < size; i++) {Namelist[i] = "N. A."; }} public string This[int index] {get {string tmp; if (index >= 0 && index <= size-1) {tmp = Namelist[index]; } else {tmp = ""; } return (TMP); } set {if (index >= 0 && index <= size-1) {namelist[i Ndex] = value; }}} public int this[string name] {get {int index = 0; while (Index < size) {if (namelist[index] = = name) {return index ; } index++; } return index; }} static void Main (string[] args) {indexednames names = new Indexednames (); Names[0] = "Zara"; NAMES[1] = "Riz"; NAMES[2] = "Nuha"; NAMES[3] = "Asif"; NAMES[4] = "Davinder"; NAMES[5] = "Sunil"; NAMES[6] = "Rubic"; Use the first indexer with an int parameter for (int i = 0; i < indexednames.size; i++) {Console.WriteLine (names[ I]); }//using the second indexer with string parameter Console.WriteLine (names["Nuha"]); Console.readkey (); } }}
When the above code is compiled and executed, it produces the following results:
Zarariznuhaasifdavindersunilrubicn. A.N. A.n. A.2
These are the contents of the C # Tutorial C # Indexer (Indexer), and more about topic.alibabacloud.com (www.php.cn)!