C#
  Home arrow C# arrow Behind the Scenes Look at C#: Indexers
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Behind the Scenes Look at C#: Indexers
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 25
    2005-08-10

    Table of Contents:
  • Behind the Scenes Look at C#: Indexers
  • C# Indexers Example (First Iteration)
  • Explanation of code
  • A Look at the MSIL Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    Behind the Scenes Look at C#: Indexers


    (Page 1 of 4 )

    Indexers help you to organize your data and get to it more quickly. In order to use one, however, there are certain steps we must take. Michael Youssef details those steps in this article.

    An index is an abstract concept. In order to gain faster access to some data that you have (in a form of object or any other construction), you index the data. In C# an index is a construction (actually a property as we will see soon) that supports the access to an internal collection (maybe an array or a collection class) of data about a given object. For example, any Department contains more than one Employee. When we want to write the Department and the Employee class, we need a way to describe that the Department class can have many Employee instances (Employees) and the client code can access these internal instances in a common way. In this situation we need an indexer to do the job.

    There are steps that we need to take. First we need to define the internal storage mechanism for the Employee instances in the Department class (we can do that by using a simple array or a complex collection class). Note that without the internal array or collection the indexer doesn't make any sense; its role to give the client code access to the internal array. Second, we need to write the indexer to provide the client code with an access mechanism. The indexer looks like this:

    public Employee this[int arg]
    {
      get
      {
        return this.Employees[arg];
      }

      set
      {
        this.Employees[arg] = value;
      }
    }

    As you can see, the indexer defines both the get and set accessors as much as a property does, and it uses the implicit argument value in the same way that a set accessor uses it. In this example, we define the indexer using the "this" keyword. The indexer's parameter is the index that you will use to get and set the value into the internal array, as we will see shortly.

    The indexer uses the keyword this to refer to the current instance of the Department object. This makes sense because it's as if we are saying "put this Employee instance in me." "Me" in this case refers to the Department object, and the indexer is the default property of the object, so "me" means "in my indexer." A class can have only one indexer, but we can overload this indexer to accept varies parameters.

    In our example, the class Department is considered to be a container class because it contains a collection of other objects and, as you will see, the relationship between the Department class and the Employee class is one-to-many. There are many Employee instances in the Department instance, and the Department instance has more than one Employee instance. This is similar to one-to-many relationships in relational database systems.

    Using indexers, we will use the index operator ([]) with our user-defined classes (the Department class in this example) in the same way as we use the index operator with the array to access its individual members. We are going to see that in the example.

    Let's take a look at our first indexer example.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway