C # indexing application-C # particularity

Source: Internet
Author: User

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 ();
}

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.