C # Use Of The indexer in the class

Source: Internet
Author: User
Indexer is a new type of class member introduced by C #. It allows objects in the class to be referenced as conveniently and intuitively as arrays. The indexer is very similar to an attribute, but the indexer can have a parameter list and can only act on instance objects, rather than directly act on classes. The class that defines the indexer allows you to use members of the [] OPERATOR category class just like accessing arrays. (Of course, there are still many advanced applications, for example, Arrays can be mapped out through the indexer)

This article only briefly demonstrates the concepts and basic usage of the indexer:

SeeCodeThe following is the definition of the class, which contains an index definition.

Class Definition
Public   Class Person
{
// Define two fields
Private   String Name;
Private   String Password;

// Define a name attribute to operate the name field
Public   String Name
{
Set {Name = Value ;}
Get { Return Name ;}
}

// Define a Password attribute to operate the password field
Public   String Password
{
Set {Password = Value ;}
Get { Return Password ;}
}

// Define the indexer. The index value of the Name field is 0, and the index value of the password field is 1.
Public   String   This [ Int Index]
{
Get
{
If (Index =   0 ) Return Name;
Else   If (Index =   1 ) Return Password;
Else   Return   Null ;
}
Set
{
If (Index =   0 ) Name = Value;
Else   If (Index =   1 ) Password = Value;
}
}
}

 

 

The following describes how to use the indexer:

Indexer usage
Protected   Void Page_load ( Object Sender, eventargs E)
{
// Declare and instantiate this class
Person P =   New Person ();

// use the indexer to assign values to the two attributes of the class
P [ 0 ] = " Jarod " ;< br> P [ 1 ] = " 123456 ,. / " ;

//Use the class property to obtain information about two fields
Label1.text=P. Name+ "/" +P. Password;
}

 

 

In the preceding class, the index value mapped to the name field of the class is 0, and the index value mapped to the password field is 1. With this ing, you can assign values to the class name and password in the following way.

P [ 0 ] =   " Jarod " ; // Set the name field value
P [ 1 ] =   " 123456 ,./ " ; // Set the password field value

 

After assigning values, you can use the attribute method to access the values assigned to the two fields.

 

 

C # indexer (1)

 

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.

Simple indexer instance:

Class Program
{
Static void main (string [] ARGs)
{
Indexclass A = new indexclass ();
A [0] = "James ";
A [1] = "Li Si ";
A [2] = "Wang Wu ";
Console. writeline ("A [0] =" + A [0]);
Console. writeline ("A [1] =" + A [1]);
Console. writeline ("A [2] =" + A [2]);
Console. readkey ();
}
}
Class indexclass
{
Private string [] Name = new string [10];
Public String This [int Index]
{
Get {return name [Index];}
Set {This. name [Index] = value ;}
}
}

Comparison between indexer and array:

The index value of the indexer is not subject to type restrictions. The index value used to access the array must be an integer, and the index can be another type of index value.

The indexer can be overloaded. A class can have multiple indexers.

The indexer does not directly store data for a variable. The indexer has get and set accessors.

 

 

C # indexer (2)

 

 

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.

Simple indexer instance:

Comparison between the indexer and attributes:

Description: The attribute is identified by name, and the indexer is identified by function signature.

The indexer can be overloaded. Properties cannot be overloaded.

The attribute can be static. The indexer belongs to an instance Member and cannot be declared as static.

Multi-parameter indexer instance:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. collections;
Namespace Study
{
Class Program
{
Static void main (string [] ARGs)
{
Scoreindex S = new scoreindex ();
S ["James", 1] = 90;
S ["James", 2] = 100;
S ["James", 3] = 80;
S ["Li Si", 1] = 60;
S ["Li Si", 2] = 70;
S ["Li Si", 3] = 50;
Console. writeline ("the score of course 3 numbered 1 is:" + s ["Zhang San", 1]);
Console. writeline ("all scores of Michael JACOB :");
Arraylist temp;
Temp = s ["James"];
Foreach (indexclass B in temp)
{
Console. writeline ("name:" + B. Name + "course No.:" + B. courseid + "score:" + B. Score );
}
Console. readkey ();
}
}
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 {This. _ name = value ;}
}
Public int courseid
{
Get {return _ courseid ;}
Set {This. _ courseid = value ;}
}
Public int score
{
Get {return _ score ;}
Set {This. _ score = value ;}
}
}
Class scoreindex
{
Private arraylist arr;
Public scoreindex ()
{
Arr = new arraylist ();
}
Public int this [String _ name, int _ courseid]
{
Get
{
Foreach (indexclass A in ARR)
{
If (A. Name = _ name & A. courseid = _ courseid)
{
Return A. Score;
}
}
Return-1;
}
Set
{
Arr. Add (New indexclass (_ name, _ courseid, value); // arr ["Zhang San", 1] = 90
}
}
// Reload the Indexer
Public arraylist this [String _ name]
{
Get
{
Arraylist temp = new arraylist ();
Foreach (indexclass B in ARR)
{
If (B. Name = _ name)
{
Temp. Add (B );
}
}
Return temp;
}
}
}
}

Note:

All indexers use the this keyword to replace the method name. Class or struct can only define one indexer and is always named this.

The indexer allows instances of classes or structures to be indexed in the same way as arrays. The indexer is similar to an attribute, but its accessors use parameters.

Get accessors return values. Set accessors allocation value.

This keyword is used to define the indexer.

The value keyword is used to define the value allocated by the set indexer.

The indexer does not need to index based on the integer value. It is up to you to decide how to define a specific search mechanism.

The indexer can be overloaded.

The indexer can have multiple parameters, for example, when accessing a two-dimensional array.

The indexer can use a hundred-value subscript, while the array can only use an integer Subscript: for example, the following defines a string Index
Public int this [string name] {...}

Attribute and Indexer

There are some differences between attributes and indexer:

Each attribute of the class must have a unique name, and each indexer defined in the class must have a unique signature) or the parameter list (so that the indexer can be reloaded ).

The attribute can be static, but the indexer must be an instance Member.

 

Related Article

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.