C # common indexer,

Source: Internet
Author: User

C # common indexer,

I have learned about the indexer before. At that time, I mixed the indexer with the attribute to think that the indexer is an attribute. The following describes the differences between the indexer and the attribute and how to use the indexer.

 

First, the index here is different from the index in the database, although they are all looking for elements.

Differences between the indexer and attributes:

 

 

Another point is that the index is very similar to an array. It allows an object to be indexed by brackets [] Like an array, but it is different from an array, specifically:

 
 

I rarely define an index in the code, but I often use it because the system has customized many indexes, such as ADO. NET for DataTable, DataRow and other classes of various traversal, search, in many places is the use of the index, for example, the following Blog Code uses a lot of system-defined indexer:

The AcceptChanges () method of the DataTable and the RowState attribute of the DataRow (both use and note of the indexer ,)

 

So how do we customize the indexer? Back to the first sentence of this blog, I used to mix the indexer and attributes, which means they are very similar. Check whether the code looks like this:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Dynamic;
// For the code here, refer to the custom consoconsoleapplicationtest {public class IndexerClass {private string [] name = new string [2]; // The indexer must be defined with the this keyword, in fact, this is the public string this [int index] {// The get method of the indexer after the class is instantiated. get {if (index <2) {return name [index] ;}return null ;}// set {if (index <2) {name [index] = value ;}} of the set Method of the Indexer ;}}}} class Program {static void Main (string [] args) {// use IndexerClass Indexer = new IndexerClass (); // assign a value to the right of "=" to the Indexer, actually, it is to call its set Method Indexer [0] = "Zhang San"; Indexer [1] = "Li Si"; // the value of the output Indexer is actually to call its get method Console. writeLine (Indexer [0]); Console. writeLine (Indexer [1]) ;}}

At first glance, it seems like the attribute, but the index has a different name and attribute name, And the this keyword is added, the special usage of this reminds me of the special position of this in the extension method.

 

The string is used as the badge, which is different from the array:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. dynamic; using System. collections; // For the code here, refer to namespace ConsoleApplicationTest {public class IndexerClass {// when using string as the index subject, use Hashtable private Hashtable name = new Hashtable (); // The indexer must be defined by the keyword this. In fact, this is the public string this [string index] {get {return name [index] object after the class is instantiated. toString ();} set {name. add (index, value) ;}} class Program {static void Main (string [] args) {IndexerClass Indexer = new IndexerClass (); indexer ["A0001"] = "Zhang San"; Indexer ["A0002"] = "Li Si"; Console. writeLine (Indexer ["A0001"]); Console. writeLine (Indexer ["A0002"]) ;}}

 

The index is overloaded. This is the difference in attributes:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. dynamic; using System. collections; // For the code here, refer to namespace ConsoleApplicationTest {public class IndexerClass {private Hashtable name = new Hashtable (); // 1: access Values public string this [int index] {get {return name [index] through key. toString ();} set {name. add (index, value) ;}// 2: Access key public int this [string aName] {get {// The actual storage of the Hashtable is DictionaryEntry (dictionary) type. to traverse a Hashtable, you need to use DictionaryEntry foreach (DictionaryEntry d in name) {if (d. value. toString () = aName) {return Convert. toInt32 (d. key) ;}} return-1 ;}set {name. add (value, aName) ;}} class Program {static void Main (string [] args) {IndexerClass Indexer = new IndexerClass (); // use Indexer [1] = "zhangsan" for the first Indexer; // use Indexer [2] = "Lisi" for the set accesser; Console. writeLine ("Name of 1:" + Indexer [1]); // use Console for get accessors. writeLine ("name 2:" + Indexer [2]); Console. writeLine (); // The use Console of the second indexer. writeLine ("the number of zhangsan is:" + Indexer ["zhangsan"]); // use the Console of the get accesser. writeLine ("the ID of Li Si is:" + Indexer ["Li Si"]); Indexer ["Wang Wu"] = 3; // set the Console for the access. writeLine ("Wang Wu's number is:" + Indexer ["Wang Wu"]) ;}}

 

Indexer with multiple parameters:

Using System; using System. collections; // For the code here, refer to the custom namespace ConsoleApplicationTest {/// <summary> /// induction Information class /// </summary> public class EntrantInfo {// name, number, department public string Name {get; set ;}public int Num {get; set ;}public string Department {get; set ;}} /// <summary> /// declare an EntrantInfo-class indexer // </summary> public class IndexerForEntrantInfo {private ArrayList ArrLst; // used to store the EntrantInfo class public IndexerForEntrantInfo () {ArrLst = new ArrayList () ;}/// <summary> // declare an indexer: search for the department information by name and number /// </summary> /// <param name = "name"> </param> /// <param name = "num"> </param> /// <returns> </returns> public string this [string name, int num] {get {foreach (EntrantInfo en in ArrLst) {if (en. name = name & en. num = num) {return en. department;} return null;} set {ArrLst. add (new EntrantInfo () {Name = name, Num = num, Department = value}) ;}/// <summary> /// declare an indexer: search for the name and department by number /// </summary> /// <param name = "num"> </param> /// <returns> </returns> public ArrayList this [int num] {get {ArrayList temp = new ArrayList (); foreach (EntrantInfo en in ArrLst) {if (en. num = num) {temp. add (en) ;}} return temp ;}// you can declare multiple versions of the indexer ...} class Program {static void Main (string [] args) {IndexerForEntrantInfo Info = new IndexerForEntrantInfo (); // this [string name, int num] use Info ["Zhang San ", 101] = "Personnel Department"; Info ["Li Si", 102] = "Administration Department"; Console. writeLine (Info ["Zhang San", 101]); Console. writeLine (Info ["Li Si", 102]); Console. writeLine (); // this [int num] Use foreach (EntrantInfo en in Info [102]) {Console. writeLine (en. name); Console. writeLine (en. department );}}}}

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.