Common Type System—Indexer Properties


Jump to: navigation, search
Visual C# Tutorials
.NET Framework Tutorials

Common Type System

© 2006 Wiley Publishing Inc.

Indexer Properties

A special variant on properties called an indexer property may be supplied. Consider this example:

class Customer
{
   private Dictionary<int, Order> orders;
 
   public Order this[int id]
   {
      get { return orders[id]; }
      set { orders[id] = value; }
   }
}

This is essentially a default property for a type, indicated by the System.Reflection.DefaultMemberAttribute in the IL. This style of property enables callers to access your object using special syntax. For example, C# permits callers to use an array-like syntax for this:

Customer c = /*...*/;
Order o = c[10010];

Indexer properties are also special in that they can be overloaded through the parameters. You may also use multiple parameters for input.


Previous_Page_.gif Next_Page_.gif





Personal tools
Share this page
  • del.icio.us
  • Facebook
  • StumbleUpon