Also Talk About Indexer

Source: Internet
Author: User

I. The indexer allows instances of classes and structures to be indexed in the same way as arrays. The indexer is similar to an attribute. The difference is that their accessors use parameters. It is called the property with parameters.

Example: simple indexer instance:

Using System;

Using System. Collections. Generic;

Using System. Linq; using System. Text;

Namespace ConsoleApplication2

{

Class Program

{

Static void Main (string [] args)

{

IndexClass Temple = new IndexClass ();

Temple [0] = "Sun San ";

Temple [1] = "ye 4 ";

Temple [2] = "Bao Wu ";

Console. WriteLine ("Champion: {0}", Temple [0]);

Console. WriteLine (": {0}", Temple [1]);

Console. WriteLine (" {: {0}", Temple [2]);

Console. ReadLine ();

}

}

Class IndexClass

{

Private string [] name = new string [100];

Public string this [int index]

{

Get {

If (index = 100)

Return name [0];

Else

Return name [index];

}

Set

{

If (index <0 | index> 100)

Name [0] = value;

Else

This. name [index] = value;

}

}

}

}

In the above example, The indexer is similar to an array, but it differs from an array in the following way:

1) the index value of the indexer is not limited by the type. The index value "one index" used to access the array is an integer.

2) The indexer can be reloaded. A class can have multiple indexers.

3) The indexer allows instances of classes and structures to be indexed in the same way as arrays. The indexer is similar to an attribute. The difference is that their accessors use parameters, which are called parameter attributes.

4) The attribute is identified by name, and the indexer is identified by function signature. The indexer can be overloaded, but attributes cannot be overloaded. The attribute can be static. The indexer belongs to an instance Member and cannot be declared as static.

In addition, the indexer can be multiple parameters, that is, the index parameters can be as follows:

Using System;

Using System. Collections. Generic;

Using System. Linq; using System. Text;

Using System. Collections;

Namespace ConsoleApplication2

{

Class Program

{

Static void Main (string [] args)

{

ScoreIndex si = new ScoreIndex ();

Si ["James", 1] = 90;

Si ["James", 2] = 100;

Si ["James", 3] = 98;

Si ["Li si", 1] = 94;

Si ["Li si", 2] = 88;

Si ["Li si", 3] = 96;

Console. WriteLine ("Zhang San's score is {0}", si ["Zhang San", 1]);

Console. WriteLine ("all scores of Michael JACOB :");

ArrayList arrTemp; arrTemp = si ["Zhang San"];

Foreach (IndexClass item in arrTemp)

{

Console. WriteLine ("Name:" + item. Name + "course No.:" + item. CourseId + "Score:" + item. Score + "\ r \ n ");

}

Console. ReadLine ();

}

}

Class IndexClass

{

Private string _ name;

Private int _ courseId;

Private int _ score;

Public IndexClass (string _ name, int _ courseId, int _ score)

{

This. _ name = _ name;

This. _ courseId = _ courseId;

This. _ score = _ score;

}

Public string Name

{

Get {return _ name ;}

Set {_ name = value ;}

}

Public int CourseId

{

Get {return _ courseId ;}

Set {_ courseId = value ;}

}

Public int Score

{

Get {return _ score ;}

Set {_ score = value ;}

}

}

Class ScoreIndex

{

Private ArrayList arr;

Public ScoreIndex ()

{

Arr = new ArrayList ();

}

Public int this [string _ name, int _ courseId]

{

Get

{

Foreach (IndexClass item in arr)

{

If (item. Name = _ name & item. CourseId = _ courseId)

{

Return item. Score;

}

}

Return-1;

}

Set

{

Arr. Add (new IndexClass (_ name, _ courseId, value ));

}

}

// Reload the Indexer

Public ArrayList this [string _ name]

{

Get {

ArrayList temp = new ArrayList ();

Foreach (IndexClass a in arr)

{

If (a. Name = _ name)

{

Temp. Add ();

}

}

Return temp;

}

}

}

}

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.