![]() |
VOOZH | about |
Prerequisite: Properties in C#
Indexers allow instances of a class or struct to be indexed just like arrays. By using indexers class will behave like a virtual array. The indexed value can be set or retrieved without explicitly specifying a type or instance member. Indexers resemble properties except that their accessors take parameters. Indexers are almost similar to the Properties. The main difference between Indexers and Properties is that the accessors of the Indexers will take parameters.
This method will give you more information, readability. If we want to retrieve information using a string as an index.
Example:
ic["username"] = "user12"; ic["password"] = "12345";
This will give more readability than the code given below.
ic[0] = "user12"; ic[1] = "12345";
Syntax:
[access_modifier] [return_type] this [argument_list]
{
get
{
// retrieval code or code to get the value
}
set
{
// code for setting value to member
}
In the above syntax:
Example:
Output:
Printing values stored in objects used as arrays UserName = user12 Password = 12345 Email = user123@gmail.com Book = CSHARP