Interface indexer (C # programming guide)

Source: Internet
Author: User

The indexer (C # programming guide) in the interface can be declared on the interface (C # reference. The accessors of the interface indexer are different from those of the class indexer in the following aspects: the interface accessors do not use modifiers. The interface accessor has no body. Therefore, the accessors are used to indicate whether the indexer is read/write, read-only, or write-only. The following is an example of the interface indexer accesser: C # copy the code public interface isomeinterface {//... // indexer Declaration: stringthis [int Index] {Get; Set ;}} the signature of an indexer must be different from the signature of all other indexers declared in the same interface. The following example shows how to implement the interface indexer. C # copy the code // indexer on an interface: public interface isomeinterface {// indexer Declaration: intthis [int Index] {Get; Set ;}// implementing the interface. class indexerclass: isomeinterface {privateint [] arr = newint [100]; publicintthis [int Index] // indexer declaration {get {// check the index limits. if (index <0 | index> = 100) {return 0;} else {return arr [Index] ;}} set {If (! (Index <0 | index> = 100) {arr [Index] = value ;}}} class mainclass {staticvoid main () {indexerclass test = new indexerclass (); // call the indexer to initialize the elements #2 and #5. test [2] = 4; test [5] = 32; For (INT I = 0; I <= 10; I ++) {system. console. writeline ("element #{0 }={ 1}", I, test [I]) ;}} output element #0 = 0 element #1 = 0 element #2 = 4 element #3 = 0 element #4 = 0 element #5 = 32 element #6 = 0 element #7 = 0 element #8 = 0 element #9 = 0 element #10 = 0 in the preceding example, you can use an explicit interface member by using the fully qualified name of the interface member. For example: Public String isomeinterface. This {}. However, to avoid ambiguity, you must use a fully qualified name only when the class uses the same indexer signature to implement more than one interface. For example, if the employee class implements two interfaces icitizen and iemployee, and these two interfaces have the same index signature, you must use an explicit interface member. That is, the following index Declaration: Public String iemployee. this {} implements the indexer on the iemployee interface, and the following declaration: Public String icitizen. this {} implements the indexer on the icitizen interface. (Source: msdn)
Related Article

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.