C # Indexer

Source: Internet
Author: User
Tags mscorlib

1. Indexer Overview

Indexers in C # provide a concise and convenient syntax that allows you to access object elements as if you were accessing an array, and typically we maintain an internal collection or array within the class that implements the indexer, using indexers to implement access to the elements in the collection. For example, I have defined a row object that internally maintains a column array, and access to the row via an array is equivalent to accessing the corresponding column element at a glance. The code is as follows:

    Public classRow {Private string[] _column;  Public intAge {Get;Set; }  PublicRow (intLen) {_column=New string[Len]; }         Public string  This[intIndex] {            Get{return_column[index];} Set{_column[index] =value;} }    }//Test Code:  classProgram {Static voidMain (string[] args) {Row R=NewRow (Ten);  for(inti =0; I <Ten; i++) {R[i]=i.tostring (); }             for(inti =0; I <Ten; i++) {Console.WriteLine ("row["+i+"]="+R[i]); }        }    }

The results of the program run:

2. The mechanism behind the indexer:

Through the above code example we need to know access to row[0] is exactly what the compiler helped us do behind the scenes, with ILDASM can see the compiler for us to generate more than two methods, one is Set_item, one is get_item, as shown:

We are looking at the Il Intermediate code generated by the main method as follows:

1. methodPrivateHidebysigStatic voidMain (string[] args) CIL managed2 {3 . EntryPoint4   //Code Size 117 (0x75)5. maxstack46. Locals init ([0]classTest1.row R,7[1] int32 i,8[2]BOOLcs$4$0000,9[3]Object[] cs$0$0001)Ten Il_0000:nop OneIl_0001:ldc.i4.sTen AIl_0003:newobj instancevoidTest1.row::.ctor (Int32) -Il_0008:stloc.0 -IL_0009:ldc.i4.0 theIl_000a:stloc.1 - IL_000B:BR.S il_0022 - Il_000d:nop -Il_000e:ldloc.0 +Il_000f:ldloc.1 - IL_0010:LDLOCA.S i +Il_0012:call instancestring[Mscorlib]system.int32::tostring ()Il_0017:callvirt instance void Test1.row::set_item (int32,23 String) - Il_001c:nop - Il_001d:nop -Il_001e:ldloc.1 -IL_001f:ldc.i4.1 - Il_0020:add inIl_0021:stloc.1 -Il_0022:ldloc.1 toIl_0023:ldc.i4.sTen + IL_0025:CLT -Il_0027:stloc.2 theIl_0028:ldloc.2 * IL_0029:BRTRUE.S il_000d $IL_002b:ldc.i4.0Panax NotoginsengIl_002c:stloc.1 - IL_002D:BR.S il_006b the Il_002f:nop +IL_0030:ldc.i4.4 A Il_0031:newarr [mscorlib]System.Object theIl_0036:stloc.3 +Il_0037:ldloc.3 -IL_0038:ldc.i4.0 $Il_0039:ldstr"row[" $Il_003e:stelem.ref -Il_003f:ldloc.3 -IL_0040:ldc.i4.1 theIl_0041:ldloc.1 - Il_0042:box [Mscorlib]system.int32WuyiIl_0047:stelem.ref theIl_0048:ldloc.3 -IL_0049:ldc.i4.2 WuIl_004a:ldstr"]=" -Il_004f:stelem.ref AboutIl_0050:ldloc.3 $IL_0051:ldc.i4.3 -Il_0052:ldloc.0 -Il_0053:ldloc.1 -  Il_0054:callvirt Instance String Test1.row::get_item (Int32) AIl_0059:stelem.ref +Il_005a:ldloc.3 theIl_005b:callstring[mscorlib] System.string::concat (Object[]) -Il_0060:callvoid[mscorlib] System.console::writeline (string) $ Il_0065:nop the Il_0066:nop theIl_0067:ldloc.1 theIL_0068:ldc.i4.1 the Il_0069:add -Il_006a:stloc.1 inIl_006b:ldloc.1 theIl_006c:ldc.i4.sTen the IL_006E:CLT AboutIl_0070:stloc.2 theIl_0071:ldloc.2 the IL_0072:BRTRUE.S il_002f the Il_0074:ret +}//end of Method Program::main

You can see that the code's 22 lines and 60 lines correspond to the statements in the source that are assigned and values for Row[i]. It is not difficult to guess that inside the code of Set_item and Get_item is a string array _column inside the row.

From this we can conclude that the principle behind the indexer is actually invoking the object's set method and get method, which is the convenience that the indexer brings to us, simplifying the way we become.

C # Indexer

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.