Garmmar:
[Access modifier] Data type this[parameter list]
{
Get
{Gets the contents of the indexer}
Set
{Set the contents of the indexer}
}
Eg:
1<span style="font-size:14px;">usingSystem; 2 usingSystem.Collections.Generic; 3 usingSystem.Text; 4 5 namespaceindexerusing6 { 7 classPhoto8 { 9 Ten Private stringname; One A Public stringName - { - Get{returnname;} the Set{name =value;} - } - PublicPhoto () {} - PublicPhoto (stringname) + { - This. Name =name; + } A } at classAlbum - { - Privatephoto[] _photos; - PublicAlbum () - { } - PublicAlbum (intcount) in { -_photos =NewPhoto[count]; to } + PublicPhoto This[intIndex] - { the Get * { $ if(Index <0|| Index >_photos. Length)Panax Notoginseng return NULL; - Else the return_photos[index]; + } A Set the { + if(Index <0|| Index >_photos. Length) - return; $ Else $_photos[index] =value; - } - } the } - Wuyi class Program the { - Static voidMain (string[] args) Wu { -Album Album =NewAlbum (3); AboutPhoto photo1 =NewPhoto ("Wang Yunpeng"); $Photo Photo2 =NewPhoto ("Huangliyun"); -Photo Photo3 =NewPhoto ("Li Wenping"); -album[0] =photo1; -album[1] =Photo2; Aalbum[2] =Photo3; +Console.WriteLine ("Enter first Photo: {0}", album[0]. Name); the - } $ } the } the</span>
C # Indexer: Take an example of an element in a collection or array _ "Go"