I\'ve seen lot of examples for c# Indexers, but in what way will it help me in real life situations.
I know the C# guru wouldn\'t have added this if it wasn\'t a ser
Addition to @code-kings post.
Moreover, calling RollNumberVariable[0]
will trigger the behavior of the default collection's indexer. While indexers are actually properties, it's on your behalf to write your own logic when extracting data. You can easily delegate most of index-parameter value to the internal collection but you can also return any arbitrary value for certain index values.
Just an example - you can have 2+ internal collections in different format, but the external user will interact with them trough a single indexer (which will kinda work as a dispatcher), while those collection will be hidden. This pretty much encourages the encapsulation principle.
http://code-kings.blogspot.in/2012/09/indexers-in-c-5.html
Indexers are elements in a C# program that allow a Class to behave as an Array. You would be able to use the entire class as an array. In this array you can store any type of variables. The variables are stored at a separate location but addressed by the class name itself. Creating indexers for Integers, Strings, Boolean etc. would be a feasible idea. These indexers would effectively act on objects of the class.
Lets suppose you have created a class indexer that stores the roll number of a student in a class. Further, lets suppose that you have created an object of this class named obj1. When you say obj1[0], you are referring to the first student on roll. Likewise obj1[1] refers to the 2nd student on roll.
Therefore the object takes indexed values to refer to the Integer variable that is privately or publicly stored in the class. Suppose you did not have this facility then you would probably refer in this way(which would be longer):
obj1.RollNumberVariable[0]
obj1.RollNumberVariable[1].
where RollNumberVariable would be the Integer variable that refers to the Roll Number of the current student object.
For more details http://code-kings.blogspot.in/2012/09/indexers-in-c-5.html
The most obvious examples, as mentioned by Skurmedel, are List<T>
and Dictionary<TKey, TValue>
. What would you prefer over:
List<string> list = new List<string> { "a", "b", "c" };
string value = list[1]; // This is using an indexer
Dictionary<string, string> dictionary = new Dictionary<string, string>
{
{ "foo", "bar" },
{ "x", "y" }
};
string value = dictionary["x"]; // This is using an indexer
? Now it may be relatively rare for you need to write an indexer (usually when you're creating a collection-like class), but I suspect you use them reasonably frequently.
You can use an indexer to elegantly provide read/write multi-threading synchronization to a non-thread safe dictionary (or any non-thread safe collection):
internal class ThreadSafeIndexerClass
{
public object this[int key]
{
get
{
// Aquire returns IDisposable and does Enter() Exit() on a certain ReaderWriterLockSlim instance
using (_readLock.Aquire())
{
object subset;
_dictionary.TryGetValue(key, out foundValue);
return foundValue;
}
}
set
{
// Aquire returns IDisposable and does Enter() Exit() on a certain ReaderWriterLockSlim instance
using (_writeLock.Aquire())
_dictionary[key] = value;
}
}
}
Useful especially when you don't want to use the heavyweight ConcurrentDictionary (or any other concurrent collection).
Let's say you have a collection of objects that you want to be able to index by something other than the order in which it was placed in a collection. In the example below, you can see how you could use the 'Location' property of some object and using the indexer, return all objects in the collection that match you location, or in the second example, all objects that contain a certain Count() of objects.
class MyCollection {
public IEnumerable<MyObject> this[string indexer] {
get{ return this.Where(p => p.Location == indexer); }
}
public IEnumerable<MyObject> this[int size] {
get{ return this.Where(p => p.Count() == size);}
}
}
I am trying to get images from a sequence file. I need some sort of a 2D array or a jagged array to hold the pixel values. I am using indexers instead of arrays because looping over indexers is faster than looping over 2D or jagged arrays.