C # Use of Indexer

Source: Internet
Author: User

1. indexer ):

The indexer allows instances of classes or structures to be indexed in the same way as arrays. The indexer is similar to an attribute. The difference is that they use parameters for access.

Use of the simplest Indexer

/// <Summary> /// the simplest indexer /// </Summary> public class idxer {private string [] Name = new string [10]; // The indexer must be defined with the keyword "this". In fact, this is the Public String This [int Index] {get {return name [Index] After Class instantiation;} set {name [Index] = value ;}} public class program {static void main (string [] ARGs) {// use idxer indexer = new idxer () for the simplest indexer; // assign a value to the indexer on the right of "=, 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]); console. readkey ();}}

 2. Differences between indexer and array:

  • The index type of The indexer is not limited to an integer:

The index value used to access the array must be an integer, and the index value type of The indexer can be defined as another type.

  • Reload allowed by the Indexer

A class is not limited to defining only one indexer. As long as the function signatures of the indexer are different, multiple indexers can be defined and its functions can be reloaded.

  • The indexer is not a variable.

The indexer does not directly define where data is stored, but an array does. Index appliances include get and set accessors.

3. Differences between the indexer and attributes:

  • The indexer is identified by the function signature method this, and the attribute is identified by the name. The name can be any
  • The indexer can be reloaded, but the attribute cannot be reloaded.
  • The indexer cannot be declared using static, but attributes can. The indexer is always a member of an instance, so it cannot be declared as static.

Use a string as the subscript to access the indexer:

// The public class idxer2 {private hashtable name = new hashtable (); // The Public String This [String Index] {get {return name [Index]. tostring ();} set {name. add (index, value) ;}} public class program {static void main (string [] ARGs) {// string-based indexer idxer2 indexer2 = new idxer2 (); indexer2 ["A01"] = "zhangsan"; indexer2 ["A02"] = "Li Si "; console. writeline (indexer2 ["A01"]); console. writeline (indexer2 ["A02"]); console. readkey ();}
}

Overload of multi-parameter indexer and Indexer

/// <Summary> /// score class /// </Summary> public class scores {/// <summary> // Student name /// </Summary> public String stuname {Get; set ;}//< summary> /// course Id /// </Summary> Public int courseid {Get; set ;} /// <summary> /// score /// </Summary> Public int score {Get; Set ;}} /// <summary> /// query the score class (Indexer) /// </Summary> public class findscore {private list <scores> listscores; Public findscore () {list Scores = new list <scores> ();} // The indexer searches for and saves the public int this [String stuname, int courseid] {get {scores S = listscores. find (x => X. stuname = stuname & X. courseid = courseid); If (s! = NULL) {return S. score ;}else {return-1 ;}} set {listscores. add (New scores () {stuname = stuname, courseid = courseid, score = value}) ;}// The indexer is overloaded, search all scores by name for public list <scores> This [String stuname] {get {list <scores> templist = listscores. findall (x => X. stuname = stuname); Return templist ;}} static void main (string [] ARGs) {// The multi-parameter indexer and indexer reload findscore FSCORE = new findscore (); FSCORE ["Zhang San", 1] = 98; FSCORE ["Zhang San", 2] = 100; FSCORE ["Zhang San", 3] = 95; FSCORE ["Li Si ", 1] = 96; // search for the score console of course number 2. writeline ("Li Si course No. 2 score:" + FSCORE ["Li Si", 1]); // search for the Score list of all three parts <scores> listscores = FSCORE ["three parts"]; If (listscores. count> 0) {foreach (scores s in listscores) {console. writeline (string. format ("Zhang San course No. {0} score: {1}", S. courseid, S. score) ;}} else {console. writeline ("no student transcript");} console. readkey ();}

-- = Source code download = --

C # Use of 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.