Using Indexers in C #

Source: Internet
Author: User
Tags arrays constructor int size valid
An indexer enables, you should use a index on an object to obtain values stored within the object. In essence this enables your to treat a object like an array.

An indexer are also similar to a. As with properties, your use get and set when defining an indexer. Unlike properties, you are not obtaining a specific data member; Rather, you are are obtaining a value from the object itself. When you define a property, your define a property name. With indexers, instead of creating a name as and with properties, and use the This keyword, which refers to the object instance and thus the object name is used. The format for defining a indexer is

Public DataType This[int Index]
{
Get
{
Do whatever your want ...
return avalue;
}
Set
{
Do whatever your want
Generally you should set a value within the class
Based on the index and the value they assign.
}
}

Creating an indexer enables for use in bracket notation ([]) with an object to set and get a value of from object. As you can the format shown earlier, where the dataType that would be set and returned by the indexer. In the ' Get ' section, and return a value this is of DataType. In the set blocks, you are able to something with a value of dataType. As with properties and a functions, you can use the value keyword. This is the value passed as the argument to the set routine. Listing 1 presents a simple example of using a indexer.

Listing 1. Using an Indexer

1://indx2.cs-using an indexer
2://--------------------------------------------------
3:
4:using System;
5:
6:public class Spellinglist
7: {
8:protected string[] words = new String[size];
9:static public int size = 10;
10:
11:public spellinglist ()
12: {
13:for (int x = 0; x < size; + +)
14:WORDS[X] = String.Format ("word{0}", x);
15:}
16:
17:public String This[int Index]
18: {
19:get
20: {
21:string tmp;
22:
23:if (Index >= 0 && index <= size-1)
24:tmp = Words[index];
25:else
26:tmp = "";
27:
28:return (TMP);
29:}
30:set
31: {
32:if (Index >= 0 && index <= size-1)
33:words[index] = value;
34:}
35:}
36:}
37:
38:public class TestApp
39: {
40:public static void Main ()
41: {
42:spellinglist myList = new Spellinglist ();
43:
44:MYLIST[3] = "=====";
45:MYLIST[4] = "Brad";
46:MYLIST[5] = "was";
47:MYLIST[6] = "here!";
48:MYLIST[7] = "=====";
49:
50:for (int x = 0; x < Spellinglist.size + + + +)
51:console.writeline (Mylist[x]);
52:}
53:}

The output from this listing is:

Word0
Word1
Word2
=====
Brad
Was
here!
=====
Word8
Word9

This listing creates a indexer to is used with the Spellinglist class. The Spellinglist class contains an array of strings called words so can is used to store a list of words. This list is set to the size of the variable declared in line 9.

Lines contain a constructor for spellinglist this sets values into each element of the array. You are could just as easily have requested the words from the reader or read them from a file. This constructor assigns the string value word## to each of the elements in the array, where # is the element number of T He array.

Jumping down to the TestApp class in lines, and you are here to the Spellinglist class are going to be used. In line, the Spellinglist class are used to instantiate the MyList object that'll hold the words. Line also causes the constructor to be executed. This initializes the word## values. Lines then change some of these values.

If you are OK to have a worked with arrays, your should be saying, ' Wait a minute ' as you look in lines to 48. To access the value of one of the words, your would normally have to access the data member within the object. When using arrays as a data member, and you learned this would assign a value to the fourth element as

MYLIST.WORDS[3] = "=====";

Line, however, are accessing the fourth element within the object, which has been the set to being the fourth element in the Wo RDS Array.

An indexer has been created for the Spellinglist class in lines to 35. This indexer enables your to access the elements within the words array using just the object name.

Line are the defining line for the indexer. You are know this is a indexer rather than a property because the This keyword is used instead of a name. Additionally, this is given a index (called index). The indexer would return a string value.

Lines contain the get portion of the indexer. The Get blocks returns a value based on the index. In this class, this value is a element from the words array. can return any value to you want. The value should, however, make sense. In line, a check are done to make sure the index value is valid. If You don't check the value of the index, you risk have an exception thrown. In this listing, if the ' index is out of the ' range, a null value is returned (line 26). If The index is valid, the value stored in the words array at the index location would be returned.

The set portion of the indexer is in lines to 34. This block can is used to set information within the object. As with properties, the value keyword contains the value being assigned. In here code, the index value is again checked to make sure it is valid. If It is, a word in the words array would be updated to the index location with the value assigned.

Looking again at the "Test class", you are the set indexer block are used to assign values in lines 48. For line, the set indexer block'll is called with value equal to ===== and is passed with a index value of 3. In line, value would be Brad and the index is 4. In line Wuyi, the get indexer-called with a index value of x. The value returned would be is the string value returned by the to indexer block.

There are times to use the indexers and There are times to don't use them. Should use indexers when it makes your code more readable and easier to understand. For example, can create a stack this can have items placed on it and taken off of it. Using an indexer for could access items within the stack without needing to remove items.



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.