C # Indexer One

Source: Internet
Author: User

An indexer allows an instance of a class or struct to be indexed in the same way as an array, with an indexer similar to an attribute, unlike an indexer whose access is a parameter.

Indexer and Array Comparisons:

(1) The indexer has an unrestricted index value (index) type

(2) Indexers allow overloading

(3) Indexer is not a variable

Different points of indexers and attributes

(1) Attributes are identified by name, and indexers are identified as functions

(2) indexers can be overloaded and properties cannot be

(3) The indexer cannot be declared static, and the property can

usingSystem;usingSystem.Collections; Public classindexerclass{Private string[] name =New string[2]; //the indexer must be defined with the This keyword, in fact this is the object after the class instantiation     Public string  This[intIndex] {        //implement the Get method of the indexer        Get        {            if(Index <2)            {                returnName[index]; }            return NULL; }        //implementing the Set method of the indexer        Set        {            if(Index <2) {Name[index]=value; }        }    }} Public classtest{Static voidMain () {//use of indexersIndexerclass Indexer =NewIndexerclass (); //The index is assigned to the right of the "=" number, which is actually called its set methodindexer[0] ="Zhang San"; indexer[1] ="John Doe"; //the value of the output indexer is actually called its Get methodConsole.WriteLine (indexer[0]); Console.WriteLine (indexer[1]); }}

 Public classindexerclass{//when using string as the indexer subscript, use Hashtable    PrivateHashtable name =NewHashtable (); //the indexer must be defined with the This keyword, in fact this is the object after the class instantiation     Public string  This[stringIndex] {        Get{returnName[index].         ToString (); Set{name. ADD (index, value); }    }} Public classtest{Static voidMain () {Indexerclass Indexer=NewIndexerclass (); indexer["A0001"] ="Zhang San"; indexer["A0002"] ="John Doe"; Console.WriteLine (indexer["A0001"]); Console.WriteLine (indexer["A0002"]); }}

 Public classindexerclass{PrivateHashtable name =NewHashtable (); //1: Access values via key     Public string  This[intIndex] {        Get{returnName[index]. ToString (); }        Set{name. ADD (index, value); }    }    //2: Access key via Values     Public int  This[stringAName] {        Get        {            //Hashtable is actually stored in the DictionaryEntry (dictionary) type, if you want to traverse a hashtable, you need to use the DictionaryEntry            foreach(DictionaryEntry Dinchname) {                if(d.value.tostring () = =aName) {                    returnConvert.ToInt32 (D.key); }            }            return-1; }        Set{name.        ADD (value, aName); }    }} Public classtest{Static voidMain () {Indexerclass Indexer=NewIndexerclass (); //use of the first type of indexerindexer[1] ="Zhang San";//use of the set accessorindexer[2] ="John Doe"; Console.WriteLine ("the name is numbered 1:"+ indexer[1]);//use of Get accessorsConsole.WriteLine ("the name is numbered 2:"+ indexer[2]);        Console.WriteLine (); //use of the second type of indexerConsole.WriteLine ("the number of the Zhang San is:"+ indexer["Zhang San"]);//use of Get accessorsConsole.WriteLine ("the number of the John Doe is:"+ indexer["John Doe"]); indexer["Harry"] =3;//use of the set accessorConsole.WriteLine ("the number of the Harry is:"+ indexer["Harry"]); }}
usingSystem;usingSystem.Collections;//onboarding Information Category Public classentrantinfo{//name, number, Department    Private stringname; Private intNumber ; Private stringDepartment;  PublicEntrantinfo () {} PublicEntrantinfo (stringNameintNumstringDepartment) {         This. Name =name;  This. Number =num;  This. Department =Department; }     Public stringName {Get{returnname;} Set{name =value;} }     Public intNum {Get{returnNumber ;} Set{Number =value;} }     Public stringDepartment {Get{returnDepartment;} Set{Department =value;} }}//to declare an indexer of a class Entrantinfo Public classindexerforentrantinfo{PrivateArrayList Arrlst;//for storing Entrantinfo class     PublicIndexerforentrantinfo () {Arrlst=NewArrayList (); }    //Declare an indexer: Find and Access department information by name and number     Public string  This[stringNameintNum] {        Get        {            foreach(Entrantinfo enincharrlst) {                if(En. Name = = Name && en. Num = =num) {                    returnen.                Department; }            }            return NULL; }        Set        {            //New keyword: C # Specifies that when you instantiate a class or call a class's constructor, you must use the new keyArrlst.add (Newentrantinfo (name, num, value)); }    }    //Declare an indexer: Find names and departments with numbers     PublicArrayList This[intNum] {        Get{ArrayList temp=NewArrayList (); foreach(Entrantinfo enincharrlst) {                if(En. Num = =num) {Temp.                Add (en); }            }            returntemp; }    }    //You can also declare multiple versions of indexers ...} Public classtest{Static voidMain () {indexerforentrantinfo Info=NewIndexerforentrantinfo (); //use of this[string name, int num]info["Zhang San",101] ="Personnel"; info["John Doe",102] ="Administration Department"; Console.WriteLine (info["Zhang San",101]); Console.WriteLine (info["John Doe",102]);        Console.WriteLine (); //use of This[int num]        foreach(Entrantinfo eninchinfo[102]) {Console.WriteLine (en).            Name); Console.WriteLine (en.        Department); }    }}

C # Indexer One

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.