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 in C# by jyoti
• 1,140 points
8 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 3 days ago by krishna
• 1,340 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 in C# by pranav
• 2,320 points
11 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 in C# by rajiv
• 1,620 points
20 views
0 votes
1 answer

When to use abstract classes

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

answered Jun 17 in C# by jyoti
• 1,140 points
10 views
0 votes
1 answer

Convert VB.NET --> C# Projects

You may decompile whole Assemblies into Visual ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
18 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,100 points
4,466 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
499 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
7,250 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 4 days ago in C# by krishna
• 1,340 points
8 views
0 votes
1 answer

How do I turn a C# object into a JSON string in .NET

Newtonsoft.json can be used to accomplish this. ...READ MORE

answered Jun 13 in C# by krishna
• 1,340 points
9 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP