Reputation: 16565
What is the real use of indexer in C#?
It looks pretty confusing as I am a new c# programmer. It looks like an array of objects but it's not.
Upvotes: 7
Views: 2343
Reputation: 16806
You can think of an indexer as a Property that takes a parameter. What you do with this parameter is up to you.
Generally, the indexers are used where providing , well, an index to your object makes sense.
For instance, if your object deals with collections of items, being able to access them using a simple '[]'
syntax makes sense because it's simple to think of it as an array to which we give the position of the object we want to return.
I wrote a blog entry a while ago on the aspects of indexers using reflection.
One of the examples I gave was:
using System;
using System.Collections;
public class DayPlanner
{
// We store our indexer values in here
Hashtable _meetings = new System.Collections.Hashtable();
// First indexer
public string this[DateTime date] {
get {
return _meetings[date] as string;
}
set {
_meetings[date] = value;
}
}
// Second indexer, overloads the first
public string this[string datestr] {
get {
return this[DateTime.Parse(datestr)] as string;
}
set {
this[DateTime.Parse(datestr)] = value;
}
}
}
And then you could use it like this:
DayPlanner myDay = new DayPlanner();
myDay[DateTime.Parse("2006/02/03")] = "Lunch";
...
string whatNow = myDay["2006/06/26"];
That's just to show you can twist the purpose of an indexer to do what you want.
Doesn't mean you should though... :-)
Upvotes: 9
Reputation: 3120
Regarding enums, if you need specific enums consider using typesafe enum pattern, see link: http://www.javacamp.org/designPattern/enum.html
Upvotes: 0
Reputation: 700910
The purpose of an indexer is just that it works like an array, but you specify code for what's happening when you read from and write to the indexer.
An indexer could do pretty much anything to provide a collection like behviour. Usually there is some kind of private collection that you offer read or read/write access to using the indexer.
An enum can not use char as it's base type, but you can use character literals to specify an integer value:
public enum Characters : int {
Alpha = 'a',
Beta = 'b'
}
Characters.Alpha will have the value 97, and Characters.Alpha.ToString() will return the string "Alpha". (char)Characters.Alpha gives the value 'a'.
Upvotes: 1
Reputation: 828200
Indexers allow instances of a class or struct to be indexed just like arrays, they are most frequently implemented in types whose primary purpose is to encapsulate an internal collection or array.
A get accessor returns a value and a set accessor assigns a value:
public T this[int i]
{
get
{
return arr[i];
}
set
{
arr[i] = value;
}
}
With the enums, they defined values are limited to a small set of primitive integral types (byte, sbyte, short, ushort, int, uint, long, ulong).
The char type is also a integral type, but it differs from the other integral types in two ways:
There are no implicit conversions from other types to the char type. In particular, even though the sbyte, byte, and ushort types have ranges of values that are fully representable using the char type, implicit conversions from sbyte, byte, or ushort to char do not exist.
Constants of the char type must be written as character-literals or as integer-literals in combination with a cast to type char. For example, (char)10 is the same as '\x000A'.
(C# Language Specification 3.0, Section 4.1.5)
Upvotes: 1
Reputation: 95522
Any collection in .NET, whether they are ArrayLists or generic Lists, have, behind the scenes arrays in their underlying structure. Indexers are merely a way to change how regular array indexes work, whether by accepting strings as an input to look for a corresponding object with that property, or some other input. But, again, underneath all that syntactic sugar, it's still ends up with a representation to an array.
This article might help explain how custom indexers are made.
On your second point, Enums are integers. If you need character representations of enums, you're limited to string manipulation, or you can decorate your enums with appropriate attributes so that they will emit your desired string or character representation.
Upvotes: 0