1. Syntax(optional)[Access modifier] Data type this[data type identifier]{get{} set{}}e.g:
1 class Program2 {3 Static voidMain (string[] args)4 {5Indexer ind =NewIndexer (5);6ind[0] =NewAnimal"Cat Department","Leopard");7ind[1] =NewAnimal"Cat Department","Leopard 1");8ind[2] =NewAnimal"Cat Department","Leopard 2");9ind[3] =NewAnimal"Cat Department","Leopard 3");Tenind[4] =NewAnimal"Cat Department","Leopard 4"); One A for(inti =0; I <5; i++) - { - Console.WriteLine (Ind[i].show ()); the } - } - } - + Public classAnimal - { + Private stringKemu; A Public stringKemu at { - Get{returnKemu;} - Set{Kemu =value;} - } - Private stringname; - Public stringName in { - Get{returnname;} to Set{name =value;} + } - Publicanimal () {} the PublicAnimalstring_kemu,string_name) * { $ This. Kemu =_kemu;Panax Notoginseng This. Name =_name; - } the Public stringShow () + { A return "name"+ Name +"account is"+Kemu; the } + } - Public classIndexer $ { $ Publicanimal[] ANI; - PublicIndexer (intcount) - { the if(Count <0) -Console.WriteLine ("The length is not legal");WuyiANI =NewAnimal[count]; the } - Wu PublicAnimal This[intIndex] - { About Get $ { - if(Index >ANI. Length) - return NULL; - returnAni[index]; A } + Set{Ani[index] =value;} the } -}
. NET Beginner's Indexer