0% found this document useful (0 votes)
6 views9 pages

Indexers in C#

Indexers in C# allow objects to be accessed using array-like syntax, enhancing code readability and simplifying data access. They can be defined with single or multiple parameters and provide controlled access to private data. While they offer advantages like intuitive data manipulation, they also have limitations such as not being able to be declared static and only allowing one indexer per class with the same signature.

Uploaded by

prazu6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Indexers in C#

Indexers in C# allow objects to be accessed using array-like syntax, enhancing code readability and simplifying data access. They can be defined with single or multiple parameters and provide controlled access to private data. While they offer advantages like intuitive data manipulation, they also have limitations such as not being able to be declared static and only allowing one indexer per class with the same signature.

Uploaded by

prazu6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

INDEXERS IN C# Presented By:

Kushala Shrestha
Prajjwal Rana Magar
What is indexers?
Indexers in C# are special class members that allow
objects to be accessed using array-like syntax. They
enable the use of square brackets [] to retrieve or
assign values to an object, just like working with
arrays. By defining an indexer, a class can act as a
virtual array, simplifying data access and enhancing
code readability.
Syntax of Indexers

public returnType this[int index]


{
get { // return value }
set { // set value }
}

• this keyword indicates the indexer.


• int index parameter specifies the index
position.
• get and set accessors manage retrieval
and assignment.
Types of Indexers
• Single Parameter Indexers: Use one parameter (e.g., int
or string).

• Multi-Parameter Indexers: Use more than one


parameter.

public string this[int x, int y]


{
get { return ... }
set { ... }
}
Working of an indexer in C#
using System;
class SampleCollection
{
private string[] elements = new string[5]; // Internal array to store data

// Indexer definition
public string this[int index]
{
get
{
return elements[index]; // Getter to retrieve value at the specified index
}
set
{
elements[index] = value; // Setter to assign value at the specified index
}
}
}
class Program
{
static void Main()
{
// Create an instance of SampleCollection
SampleCollection collection = new SampleCollection();
// Assign values using the indexer
collection[0] = "Hello";
collection[1] = "World";

// Retrieve and print values using the indexer


Console.WriteLine(collection[0]); // Output: Hello
Console.WriteLine(collection[1]); // Output: World
}
}
Features of Indexers
• Indexers are like properties but require parameters to
access values.
• Indexers can be overloaded by defining multiple
indexers with different parameter signatures.
• Indexers can use int, string, or even custom types as
indices.
• They allow objects to be accessed using square
brackets [], making data retrieval and manipulation
intuitive.
• Indexers provide controlled access to private data,
enhancing encapsulation while maintaining simplicity.
Advantages of Indexers
• Provide a cleaner and more intuitive way to work with
data structures.
• Simplify access to collections and class data.
• Enhance encapsulation while providing flexibility.

Limitations of Indexers
• Cannot be declared static.
• Only one indexer can exist per class with the same
signature.
• Limited to get and set operations.
Conclusion
Indexers in C# provide a powerful and intuitive
way to allow objects to be accessed like arrays,
enhancing both the flexibility and readability of
your code. They are particularly useful when
working with custom collections or data
structures, enabling developers to manage
internal data with array-like syntax.
Understanding how to effectively use indexers
allows you to write more efficient and
maintainable code while balancing their potential
drawbacks.
THANK
YOU

You might also like