C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,160 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,529 people online right now. Registration is fast and FREE... Join Now!




> Indexers

e_barroga
Group Icon



post 20 Aug, 2009 - 10:26 AM
Post #1


Before reading on with this tutorial, please be sure you have an understanding of the following concepts:
- Brief understanding of properties (indexers share some similarities).
- C# get and set accessors.
- The implicit variable "value".
- The formal parameter list.


Indexers is a feature in C# that allows you to treat an object like an array. By using indexers you have the ability to access members through indexes. Indexers are also known as smart arrays in the C# community. Indexers are basically members that allow an object to be indexed the same way an array.

The format for indexers is as follows:
CODE

<modifier> <return type> this [parameter list]
{
    set
    {
        // Set code.
    }

    get
    {
        // Get code.
    }
}


The modifier can be: public, private, protected or internal. The return type can be any valid C# type. The formal parameter list must have atleast on parameter. ref and out parameter modifiers are not permitted. It is also not possible to define a static indexer.


Here is a quick example demonstrating the construction of indexers:
CODE

class foo
{
    // The field member our indexer will be accessing.
    private string[] data = new string[3];

    // The indexer.
    public string this [int index]
    {
        set
        {
            data[index] = value;
        }

        get
        {
            return data[index];
        }
    }
};


Now, our object can be accessed through indexes:
CODE

foo bar = new foo();

bar[0] = "Element 1";
bar[1] = "Element 2";
bar[2] = "Element 3";


Here is another example of indexers, indexing two field members:
CODE

class MyClass
{
    private int Field1;
    private int Field2;

    public int this [int index]
    {
        get
        {
            if (index == 0)
                return Field1;

            else
                return Field2;
        }

        set
        {
            if (index == 0)
                Field1 = value;

            else
                Field2 = value;
        }
    }
}



Indexers can be overloaded like any other type of member. To overload an indexer, its signature must differ. The signature of an indexer is constructed from the number and types of its formal parameters. It isn't sufficient for the indexer type to be different. The parameter lists must be different.

The following is an example of indexer overloading:
CODE

class MyClass
{
    public string this [ int index ]
    {
        get { ... }
        set { ... }
    }

    public string this [ int index1, int index2 ]
    {
        get { ... }
        set { ... }
    }

    public int this [ float index1 ]
    {
        get { ... }
        set { ... }
    }
}


Note the differences between indexers and properties:
- Properties are identified by their names, but an indexer is identified by its signature.
- Indexers are accessed through element access. Properties are accessed through member access.
- Indexers are always an instance member, but properties may be static.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Posts in this topic
e_barroga   Indexers   20 Aug, 2009 - 10:26 AM


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 05:58PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month