When should you use C indexers

0 votes
I'd like to utilise indexers more often, but I'm not sure when it's appropriate to do so. All of the examples I've found online use classes like MyClass and IndexerClass.

Is there a need for indexers in a school system where there are Students and Teachers, and each Teacher has a list of Students that they are responsible for? For the sake of simplicity, each Student can only have one Teacher.
Jun 11, 2022 in C# by jyoti
• 1,240 points
417 views

1 answer to this question.

0 votes

First, let's talk about indexer. Class (or struct) instances can be indexed like array elements thanks to a highly specialised attribute called the indexer (properties can be static but indexers cannot).

Now, why should you use indexer

  1. The class becomes the data structure instead of the new data structure
  2. Syntax is easy

With that said, when shoule you use indexer? If your class requires list or arrays of its instances. Here's a code to show this

public class Person{
    public string Name{get; set;}
    
    private Person[] _backingStore;
    public Person this[int index]
    {
        get{
            return _backingStore[index];
        }
        set{
            _backingStore[index] = value;
        }
    }
}

Person p = new Person();
p[0] = new Person(){Name = "Bostov"};
p[1] = new Person(){Name = "Mindy Tow"};    
answered Jun 23, 2022 by krishna
• 2,820 points

Related Questions In C#

0 votes
1 answer

When would you use delegates in C#

Delegates come in handy for a variety ...READ MORE

answered Jun 11, 2022 in C# by pranav
• 2,590 points
529 views
0 votes
1 answer

Why should I avoid using Properties in C#?

Jeff dislikes properties because they resemble fields, ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
638 views
0 votes
1 answer

When to use abstract classes

When you need a class for inheritance ...READ MORE

answered Jun 17, 2022 in C# by jyoti
• 1,240 points
551 views
0 votes
0 answers

When to use an interface instead of an abstract class and vice versa

This could be an OOP question in ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
363 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
5,681 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,088 views
+6 votes
16 answers

How do backend of these really cool games work?

Most of the games these days don't ...READ MORE

answered Jul 19, 2018 in Career Counselling by Kalgi
• 52,350 points
11,233 views
0 votes
1 answer
0 votes
1 answer

What does the extension method do in c# and why do you need it

Extension Methods allow you to add additional ...READ MORE

answered Jun 21, 2022 in C# by krishna
• 2,820 points
640 views
0 votes
1 answer

How and when to use ‘async’ and ‘await’

The compiler creates a state machine in ...READ MORE

answered Jul 4, 2022 in C# by krishna
• 2,820 points
529 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP