C # Tutorial C # indexer (Indexer)

Source: Internet
Author: User

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)!

  • Related Article

    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.