
How to Use Indexer in Classes and Interfaces of C# (C Sharp)Does something come to your mind when you read the word indexer? Hope you are recalling index of array elements at this moment. This indexer is not referring to index of array element but it is used to treat a class or interface or struct as an array. This article will guide on understanding the concept of indexers and how to use indexer in classes and interfaces.
Indexers
can be declared using the following syntax: Here are
the basic guidelines to use Indexers: Indexers
do not have a name. However if you want to give it a name so that languages
other than C# can access your indexer, you can do it as shown below: [System.Runtime.CompilerServices.IndexerName("TestIndexer")] The indexer
shown in this example can be identified using the name TestIndexer. Indexers
are accessed using the instance pointer this. They are directly
accessed using the instance name. Indexers
have two access methods, get method is used to fetch values
and do manipulations. set method is used to set value to an
attribute. Any or all of these methods can be used based on the access
privilege of the indexer: read-only or write-only or read-write. Parameters
of an Indexer can be of any type, not just integer. Indexers
can never be static. Usage of
Indexer in Classes: You will
understand how to use indexer in classes by browsing the following example: public class
testIndexer { public string
this[int index1, int index2] { This example
defines an indexer to fetch and store elements into a two dimensional
array test2DArray. This indexer defined in testIndexer is then accessed
in the class useIndexer. If you look at the way the indexer is used, you
will understand that instead of accessing the array element as testObj.test2DArray[0,0]
you can directly access it as testObj[0,0] as if the instance in itself
is an array. This is achieved using indexers. They provide the same functionality
but with less complexity and easy understanding in accessing the element.
Not just arrays, you can do any manipulation inside indexers by receiving
suitable parameters. Note that
the above code includes range checks for index1 and index2. This range
check avoids unwanted occurrence of exceptions. This ensures robustness
and reliability of your code. Usage of
Indexer in Interfaces: Indexer can
be used in interfaces. For using it, guidelines are same as the ones indicated
in the beginning of this article. In addition, two more constraints are
enforced as shown below: Indexer
in class will have access modifier associated with it. But indexer in
interfaces should not include access modifiers. When the interface is
implemented by a class, the class can override the indexer with modifiers.
But the indexer declared in interface should not include any modifiers. Syntax for
using indexer in interface is shown below: public interface
testInterface { Here you
are defining an interface called testInterface containing indexer declaration.
You are then using testIndexer class to implement this interface and this
class contains the actual definition of the indexer. Once when the indexer
is defined, you are accessing it from useIndexer class. This example will
fetch the same results as that of earlier example.
Tweet
_______________________________________________________________________ _______________________________________________________________________
FREE
Subscription
Subscribe to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |