Analysis on the use of Indexer

Source: Internet
Author: User

The index function is a further extension of attributes. It can control the reading and writing of multiple variables in an array. The popular point is that you can beat a cow in the mountains. You can also beat a cow directly, but it may be difficult to control the intensity. The indexer achieves this effect. Let's take a look at the implementation below.Code:

 

Code
Using System;
Class Indexerclass
{
Private   Int [] Myarray =   New   Int [ 100 ];
Public   Int   This [ Int Index] // Indexer Declaration
{
Get
{ // Check the index limits.
If (Index <   0   | Index > = 100 )
Return 0 ;
Else
Return Myarray [Index];
}
Set
{
If ( ! (Index <   0   | Index > =   100 ))
Myarray [Index] = Value;
}
}
}
  Public   Class Mainclass
{
Public   Static   Void Main ()
{
Indexerclass B =   New Indexerclass ();
// Call the indexer to initialize the elements #3 and #5.
B [ 3 ] =   256 ;
B [ 5 ] =   1024 ;
For ( Int I = 0 ; I <= 10 ; I ++ )
{
Console. writeline ( " Element #{0 }={ 1} " , I, B [I]);
}
}
}

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.