Indexer (programming)

In object-oriented programming, an indexer allows instances of a particular class or struct to be indexed just like arrays.[1] It is a form of operator overloading.

Implementation

Indexers are implemented through the get and set accessors for the operator[]. They are similar to properties, but differ by not being static, and the fact that indexers' accessors take parameters. The get and set accessors are called as methods using the parameter list of the indexer declaration, but the set accessor still has the implicit value parameter.

Example

Here is a C# example of the usage of an indexer in a class: [2]

class OurFamily
{
	public OurFamily(params string[] pMembers)
	{
	    familyMembers = new List<string>();
	    familyMembers.AddRange(pMembers);
	}
	
	private List<string> familyMembers;
	
	public string this[int index]
	{
		// The get accessor
		get
		{
		    return familyMembers[index];
		}

		// The set accessor with 
		set
		{
		    familyMembers[index] = value;
		}
	}

	public int this[string val]
	{
		// Getting index by value (first element found)
		get
		{
		    return familyMembers.FindIndex(m => m == val);
		}
	}

	public int Length => familyMembers.Count;
	
}

Usage example:

void Main()
{
    var doeFamily = new OurFamily("John", "Jane");
    for (int i = 0; i < doeFamily.Length; i++)
    {
        var member = doeFamily[i];
        var index = doeFamily[member]; // same as i in this case, but it demonstrates indexer overloading allowing to search doeFamily by value.
        Console.WriteLine($"{member} is the member number {index} of the {nameof(doeFamily)}");
    }
}

In this example, the indexer is used to get the value at the nth position, and then to get the position in the list referenced by its value. The output of the code is:

  John is the member number 0 of the doeFamily
  Jane is the member number 1 of the doeFamily
gollark: ?tag create dft The DFT is the most important discrete transform, used to perform Fourier analysis in many practical applications.[1] In digital signal processing, the function is any quantity or signal that varies over time, such as the pressure of a sound wave, a radio signal, or daily temperature readings, sampled over a finite time interval (often defined by a window function[2]). In image processing, the samples can be the values of pixels along a row or column of a raster image. The DFT is also used to efficiently solve partial differential equations, and to perform other operations such as convolutions or multiplying large integers.
gollark: I really should thingy esobot one of these days.
gollark: According to our apiarists, muting ABR is actually mean, due to its new search command.
gollark: They should probably be one cog.
gollark: Which is also why the entire discord link module has to be reloaded when a webhook is added. It's actually bad.

See also

References

  1. jagadish980 (2008-01-29). "C# - What is an indexer in C#". http://forums.sureshkumar.net/forum.php: Bulletin: SURESHKUMAR.NET FORUMS. Archived from the original on September 22, 2009. Retrieved 2011-08-01.
  2. "C# Interview Questions". http://www.dotnetfunda.com/: .net Funda. Retrieved 2011-08-01.


This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.