Tutorials
Forum
Resources
Reviews
Jobs
Interview
Videos
Communities
Projects
Training
Silverlight Games
|
Bookmarks
|
Mentor
|
Code Converter
|
IT Companies
|
Peer Appraisal
|
Members
|
Polls
|
Revenue Sharing
|
Computer Jokes
|
New Posts
|
Lobby
|
Gift Shop
|
Online Members
Anu George
NareshGodera
N. KUMAR
Naveen
Mahesh
Shesh Kumar Mishra
Pawan Awasthi
saranya
raju
Anil Kumar Pandey
sudheera
More...
Resources
»
Articles
»
.NET Framework
What are Indexers
Posted Date:
01-Sep-2004
Category:
.NET Framework
Rating:
Author:
G.B Naveen Kumar
Member Level:
Bronze
Points
: 5
This article explains Indexers in C#
Defining an indexer allows you to create classes that act like "virtual arrays." Instances of that class can be accessed using the [] array access operator. Defining an indexer in C# is similar to defining operator [] in C++, but is considerably more flexible. For classes that encapsulate array- or collection-like functionality, using an indexer allows the users of that class to use the array syntax to access the class.
Example
The following example shows how to declare a private array field, myArray, and an indexer. Using the indexer allows direct access to the instance b[i]. The alternative to using the indexer is to declare the array as a public member and access its members, myArray[i], directly.
// cs_keyword_indexers.cs
using System;
class IndexerClass
{
private int [] myArray = new int[100];
public int this [int index] // Indexer declaration
{
get
{
// Check the index limits.
if (index < 0 || index >= 100)
return 0;
else
return myArray[index];
}
set
{
if (!(index < 0 || index >= 100))
myArray[index] = value;
}
}
}
public class MainClass
{
public static void Main()
{
IndexerClass b = new IndexerClass();
// Call the indexer to initialize the elements #3 and #5.
b[3] = 256;
b[5] = 1024;
for (int i=0; i<=10; i++)
{
Console.WriteLine("Element #{0} = {1}", i, b[i]);
}
}
}
Did you like this resource? Share it with your friends and show your love!
Tweet
Share
Responses to "What are Indexers"
Author:
desifella
03 Sep 2004
Member Level:
Bronze
Points
: 0
Hi Naveen,
Even though the article helped me to recollect what indexers are it doesn't deal with all the info related to indexers(like what are the restrictions on having multiple indexers and why is it a good thing and a real world scenario). It will be nice if you try to make this as comprehensible as possible. Good effort though.
-desi
Author:
Kamran
20 Apr 2005
Member Level:
Bronze
Points
: 0
can i use indexers in .net frame work 1.1?? & can we use this approach in vb.net?
Feedbacks
Post Comment:
Notify me by email when others post comments to this article.
Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
No HTML formatting and links to other web sites are allowed.
This is a strictly moderated site. Absolutely no spam allowed.
Name:
Sign In
to fill automatically.
Email:
(Will not be published, but
required
to validate comment)
Type the numbers and letters shown on the left.
Next Resource:
Customizing Window Form Data Grid
Previous Resource:
How to convert XML to Dataset and Dataset to XML
Return to Resources
Post New Resource
Category:
.NET Framework
Post resources and
earn money
!
More Resources
Customze Standard Wrapper
Datagrid Binding directly by XML
Papulate a TreeView with two DataTables(Which are having relation between them)
Calling server side functions in Datagrid Template column
String.Intern
Generating C# Class File(Sample.cs) Dynamically using CodeDom
Popular Tags
Tag posting guidelines
Search Tags
(No tags found.)
My Profile
Sign In
Register
AdSense Revenue
Active Members
Today
Asheej T K
(85)
Ajatshatru Upa...
(48)
Pawan Awasthi
(31)
Last 7 Days
Asheej T K
(327)
Paritosh Mohap...
(289)
Ajatshatru Upa...
(253)
more...
Awards & Gifts
Email subscription
.NET Jobs
.NET Articles
.NET Forums
Articles Rss Feeds
Forum Rss Feeds
About Us
Trademark Disclaimer
Contact Us
Copyright
Privacy Policy
Terms Of Use
Revenue Sharing sites
Advertise
Talk to
Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.