C # functions and usage of the Indexer

Source: Internet
Author: User

1. role:

Classes and instances can be used like arrays, which are also called attributes with parameters.

2. Differentiation

(1) 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.
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.

(2) 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.

3. Use

(1) Example 1

string str = "abc";char c = str[0];

(2) Example 2

Static void main (string [] ARGs) {indexclass = new indexclass (); indexclass [0] = "Wang Wu"; indexclass [1] = "Zhao Si ";} class indexclass () {private string [] Name = new string [10]; Public String This [int Index] {get {return name [Index];} set {name [Index] = value ;}}}

 

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.