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.
An indexer is a special type of property that allows a class or a structure to be accessed like an array for its internal collection. C# allows us to define custom indexers, generic indexers, and also overload indexers. An indexer can be defined the same way as property with this keyword and square brackets [] .
Fundamentally, an indexer is a pair of methods (or a single one) whereas an array access gives you a storage location. Note that if you weren't using a mutable struct to start with, you wouldn't see the difference in this way - I'd strongly advise against using mutable structs at all.
Which among the following are the advantages of using indexers? Explanation: Indexers provides a view at large scale to visualize a collection of items as an array. It is really easy to use the object of the class that represents a collection as if it is an array.
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... :-)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With