C # Indexer

Source: Internet
Author: User

1. Description

The indexer allows instances of classes or structures to be indexed like arrays. The indexer is similar to an attribute, but its accessors use parameters.

    • You can use the indexer to create an index for an object in an array-like manner.

    • GetAccessors return values.SetThe value allocated by the accessor.

    • This keyword is used to define the indexer.

    • The value keyword is used to defineSetThe value allocated by the 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.

2. ExampleCode

Namespace csharpdemo {class program {static void main (string [] ARGs) {studentscore SC = new studentscore (); console. writeline ("score of the first student is {0}", SC [0]); console. readline () ;}/// <summary> // example of the indexer /// </Summary> public class studentscore {private double [] SC = {90, 88, 67, 41, 67, 72, 98}; private string [] names = {"Li", "Xu", "Zhang", "Wang", "Chen ", "Wu", "Hu"}; Public double this [int I] {get {return SC [I];} set {SC [I] = value ;}} // you can have multiple indexers. Public String This [string name] {get {return getname (name) ;}} private string getname (string name) is supported only when the parameters are different) {foreach (string s in names) {If (S = Name) {return s ;}} return string. empty ;}}}

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.