What is the use of indexers?

What is the actual use of an indexer in C #?

This seems rather confusing as I am a new C # programmer. It looks like an array of objects, but it is not.

+6
c #
source share
5 answers

You can think of the indexer as an object that takes a parameter. What you do with this option is up to you.

Typically, indexers are used where provided, well, the index for your object makes sense.

For example, if your object deals with collections of elements, the ability to access them using the simple '[]' syntax makes sense, because just thinking of it as an array, to which we give the position of the object we want to return.

I recently wrote a blog post about aspects of indexers that use reflection .
One example that 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 can use it as follows:

  DayPlanner myDay = new DayPlanner(); myDay[DateTime.Parse("2006/02/03")] = "Lunch"; ... string whatNow = myDay["2006/06/26"]; 

To show that you can flip the indexer target to do what you want.
This does not mean that you should ... :-)

+8
source share

Indexers allow you to index instances of a class or structure in the same way as arrays; they are most often implemented in types whose main purpose is to encapsulate an internal collection or array.

A get accessor returns a value, and a set accessory assigns a value:

 public T this[int i] { get { return arr[i]; } set { arr[i] = value; } } 

With enumerations, they define values ​​limited by a small set of primitive integral types (bytes, sbyte, short, ushort, int, uint, long, ulong).

The char type is also an integral type, but it differs from other integral types in two ways:

  • There are no implicit conversions from other types to char. In particular, although 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.

  • Char constants must be written as character literals or as integer literals in combination with cast char. For example, (char) 10 matches "\ x000A".

( C # 3.0 Language Specification, Section 4.1.5 )

+1
source share

The purpose of the indexer is that it works like an array, but you specify the code for what happens when you read and write to the index.

An indexer could do just about anything to provide a collection like behviour. Usually there is some kind of private collection that you offer for reading or reading / writing using an indexer.

An enumeration cannot use char as a 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 a value of 97, and Characters.Alpha.ToString () will return the string "Alpha". (char) .Alpha characters gives the value "a".

+1
source share

Any collection in .NET, whether it be ArrayLists or general lists, has arrays behind the scenes in its basic structure. Indexers are just a way to change how regular array indexes work, regardless of whether you accept strings as input to find the corresponding object with this property or some other input. But, again, under all this syntactic sugar, it still ends with an array representation.

This article can help explain how custom indexes are created .

At your second point, Enums are integers. If you need symbolic representations of enumerations, you are limited to string manipulations or you can decorate your enumerations with appropriate attributes so that they produce the desired representation of a string or character.

0
source share

Regarding enums, if you need specific enums, consider using the enum enum template, see link: http://www.javacamp.org/designPattern/enum.html

0
source share