Indexer C #

Source: Internet
Author: User
Tags modifiers

Overview

Indexers allow instances of a class or struct to be indexed just like an array. indexers are similar to properties, except that their accessors take parameters.

In the following example, a generic class is defined and a simple get and set accessor method (as a method of assigning and retrieving values) is provided.The program class creates an instance of this class for the storage string.

Code
1 class Samplecollection<t>
2 {
3//Declare an array to store the data elements.
4 private t[] arr = new t[100];
5
6//Define the indexer, which would allow client code
7//to use [] notation on the class instance itself.
8//(see Line 2 of the code in Main below.)
9 public T This[int i]
10 {
One get
12 {
//This indexer are very simple, and just returns or sets
//The corresponding element from the internal array.
return arr[i];
16}
Set
18 {
Arr[i] = value;
20}
21}
22}
23
//This class shows how client code uses the indexer.
Class Program
26 {
static void Main (string[] args)
28 {
//Declare An instance of the Samplecollection type.
samplecollection<string> stringcollection = new samplecollection<string> ();
31
[] notation on the type.
Stringcollection[0] = "Hello, world";
System.Console.WriteLine (Stringcollection[0]);
35}
36}

Indexer and attribute comparisons

Property

Indexer

Allows methods to be called like public data members are called.

Allows you to use array notation on an object itself to access the elements in the inner collection of that object.

Access is available through a simple name.

Access is available through the indexer.

Can be a static member or an instance member.

Must be an instance member.

get accessor of a property has no parameters." The get  accessor for a property has no parameters.

get Accessor of an indexer have the same formal parameter list as the indexer. " The  get  accessor of the indexer has the same formal parameter list as the indexer.

set accessor of a property contains the implicit value parameter." The set accessor of a property contains an implicit  value  parameter.

set accessor of an indexer have the same formal parameter list as the indexer, and also to the value parameter." > In addition to the value parameter, the  set  accessor of the indexer also has the same formal parameter list as the indexer.

Supports the use of the phrase method for auto-implemented attributes.

The phrase method is not supported.

Using indexers in interfaces

Indexers can be declared on an interface.The accessors of the interface indexer differ from the accessors of the class indexer in the following ways:

    • Interface accessors do not use modifiers.

    • The interface accessor has no body.

Therefore, the purpose of the accessor is to indicate whether the indexer is read-write, read-only, or write-only.

An example of an interface indexer accessor:

1 public Interface ISomeInterface
2 {
3 //...

5 string This[int index]
6 {
7 get;
8 set;
9 }
10}
How to implement an excuse indexer sample
1//Indexer on an interface:
2 Public Interface ISomeInterface
3 {
4//Indexer declaration:
5 int This[int Index]
6 {
7 get;
8 set;
9}
10}
11
//Implementing the interface.
Class Indexerclass:isomeinterface
14 {
[Private int[] arr = new int[100];
+ public int This[int index]//Indexer Declaration
17 {
Get
19 {
//The Arr object would throw IndexOutOfRange exception.
return Arr[index];
22}
Set
24 {
Arr[index] = value;
26}
27}
28}
29
Class MainClass
31 {
static void Main ()
33 {
Indexerclass test = new Indexerclass ();
System.Random rand = new System.Random ();
The indexer to initialize its elements.
PNS for (int i = 0; i <; i++)
38 {
Test[i] = rand. Next ();
40}
for (int i = 0; i <; i++)
42 {
System.Console.WriteLine ("Element #{0} = {1}", I, test[i]);
44}
45
Keep//The console window open in debug mode.
System.Console.WriteLine ("Press any key to exit.");
System.Console.ReadKey ();
49}
50}
*/* Sample output:
Element #0 = 360877544
Element #1 = 327058047
Si Element #2 = 1913480832
Element #3 = 1519039937
Element #4 = 601472233
Element #5 = 323352310
Element #6 = 1422639981
Element #7 = 1797892494
Element #8 = 875761049
Element #9 = 393083859
62 */

In the example above, explicit interface member implementations can be used by using the fully qualified name of the interface member. For example:



However, a fully qualified name is required to avoid ambiguity only if the class implements more than one interface using the same indexer signature.For example, if the Employee class implements two interfaces ICitizen and IEmployee, and the two interfaces have the same indexer signature, an explicit interface member implementation must be used. that is, the following indexer declares:



implements the indexer on the IEmployee interface, and the following declaration:



implements the indexer on the ICitizen interface.

"This article was published by Mr. White, May 08, 2017"

Indexer C #

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.