SIGN UP MEMBER LOGIN:    
ARTICLE

C# Indexer

Posted by Pramod Singh Articles | Articles C# January 15, 2000
Tags: C#, Indexer
An indexer is a member that enables an object to be indexed in the same way as an array.
Reader Level:
Download Files:
 

An indexer is a member that enables an object to be indexed in the same way as an array. Indexers are declared using indexer-declarations:
indexer-declaration:
attributesopt indexer-modifiersopt indexer-declarator
accessor-declarations
indexer-modifiers:
indexer-modifier
indexer-modifiers indexer-modifier
indexer-modifier:
new
public
protected
internal
private
virtual
override
abstract
indexer-declarator:
type this [ formal-parameter-list ]
type interface-type . this [ formal-parameter-list ]
An indexer-declaration may include a set of attributes , a new modifier, a valid combination of the four access modifiers, and one of the virtual , override , or abstract modifiers. The type of an indexer declaration specifies the element type of the indexer introduced by the declaration. Unless the indexer is an explicit interface member implementation, the type is followed by the keyword this. For an explicit interface member implementation, the type is followed by an interface-type, a ".", and the keyword this. Unlike other members, indexers do not have user-defined names.

The formal-parameter-list specifies the parameters of the indexer. The formal parameter list of an indexer corresponds to that of a method , except that at least one parameter must be specified, and that the ref and out parameter modifiers are not permitted. The type of an indexer and each of the types referenced in the formal-parameter-list must be at least as accessible as the indexer itself The accessor-declarations, which must be enclosed in "{" and "}" tokens, declare the accessors of the indexer. The accessors specify the executable statements associated with reading and writing indexer elements.

Even though the syntax for accessing an indexer element is the same as that for an array element, an indexer element is not classified as a variable. Thus, it is not possible to pass an indexer element as a ref or out parameter.

The formal parameter list of an indexer defines the signature  of the indexer. Specifically, the signature of an indexer consists of the number and types of its formal parameters. The element type is not part of an indexer's signature, nor are the names of the formal parameters. The signature of an indexer must differ from the signatures of all other indexers declared in the same class.

Indexers and properties are very similar in concept, but differ in the following ways:
A property is identified by its name, whereas an indexer is identified by its signature. A property is accessed through a simple-name  or a member-access , whereas an indexer element is accessed through an element-access . A property can be a static member, whereas an indexer is always an instance member. A get accessor of a property corresponds to a method with no parameters, whereas a get accessor of an indexer corresponds to a method with the same formal parameter list as the indexer. A set accessor of a property corresponds to a method with a single parameter named value, whereas a set accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus an additional parameter named value. It is an error for an indexer accessor to declare a local variable with the same name as an indexer parameter. With these differences in mind, all rules defined in  apply to indexer accessors as well as property accessors. To under stand these concepts please see the Sample code in C#.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor