What are indexers in C#
An indexer allows an object to be indexed like an array. It is useful to support the creation of specialized arrays respective to one or more constraints. Indexers allows a class to be accessed the same way as an array is access means object of a class be indexed same way as an array. Modifier of an indexer can be of public, private, protected and internal. The return type can be any valid C# types . The indexing is performed using the [ ] operator.
An indexer is also similar to a property. As with properties, you use get and set when defining an indexer . Unlike properties, you are not obtaining a specific data member; rather, you are obtaining a value from the object itself.
Regardless of the internal implementation of the class, its data can be obtained consistently through the use of indexers. Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error .
Example
When should you use C# indexers?
You can typically use an indexer if the class represents a list, collection or array of objects.
- Does C# support multiple Inheritance ?
- What is Process ID ?
- How do I make a DLL in C# ?
- How many ways you can pass values to Windows Services ?
- Can we use "continue" statement in finally block ?
- What is nullable type in c# ?
- Difference between the Debug class and Trace class ?
- What is lock statement in C#
- What are dynamic type variables in C#
- What is the difference between is and as operator in C#?
- What are circular references in C#?
- What are the differences between events and delegates in C#
- Explain the types of unit test cases in C#?
- How many types of comments are there in C#?
- What are the various ways to pass parameters to a method in C#?
- What are the different ways to deploy a assembly in net?
- What does assert() method do in c#
- What is literals in C# - Constants and Literals
- What is the use of goto statement in C#
- How can JIT code be faster than AOT compiled code
- Why events have no return types in .NET
- What's the difference between a static method and a non-static method in C#
- What's a weak reference c#?
- What is C# equivalent of the vb.net isNothing function
- What are generics in c#