C # indexer (1)

Source: Internet
Author: User

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.

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.