We know that an array stores a series of related data types and provides a powerful indexing method to obtain its value: Index by array name and index subscript.
C # also provides powerful indexing functions that are not available in other languages. C # uses indexes to access its own members.
Let's look at an instance.
First, I defined a user class, including fields, attributes, and methods.
Class user
{
Private string name;
Private string password;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
Public String Password
{
Get {return password ;}
Set {Password = value ;}
}
Public void sayhi ()
{
Console. writeline ("Your name is {0}, your password is {1}", name, password );
}
}
Second: I have defined a generic class and defined the indexer in the class.
Class userset <t>
{
Private T [] arr = new T [0];
Public t this [int I]
{
Get {return arr [I];}
Set {arr [I] = value ;}
}
Third: start to implement the index
Static void main ()
{
// The generic class contains the user type.
Userset <user> Users = new userset <user> ();
// Index user data members through generic Indexes
Users [0]. Name = "Xiaogang ";
Users [0]. Password = "123456 ";
Users [0]. sayhi ();
}