Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you use C# indexers?

Tags:

c#

indexer

I'd like to use indexers more, but I'm not sure when to use them. All I've found online are examples that use classes like MyClass and IndexerClass.

What about in a school system where there are Students and Teachers, and each Teacher has a list of Students that they're in charge of - any need for indexers in that scenario? For simplicity's sake: each Student can only belong to one Teacher.

like image 979
Ian Davis Avatar asked Jul 23 '10 03:07

Ian Davis


People also ask

When should I use C?

As a middle-level language, C combines the features of both high-level and low-level languages. It can be used for low-level programming, such as scripting for drivers and kernels and it also supports functions of high-level programming languages, such as scripting for software applications etc.

When would you choose C rather than C++?

However, if you need to code truly tiny systems, using C will result in less overhead than C++. C++ is well-rounded in terms of platforms and target applications, so if your project is focused on extremely low-level processing, then you may want to use C++.

Why would you use C instead of C++?

C is procedural and does not support classes and objects, meaning it has less functionality than C++. This allows you to spend more time focusing on what you can do with C's libraries, especially at the OS level. With C++ having roots in C's code, learning C will only make studying C++ that much easier down the road.

Is C easier than C++?

We can say that C is a hands-on language and we can program it in whichever way we want. C++ consists of some high-level object-oriented programming constructs that help us to code high-level programs. Thus if we say C is easy then C++ is also easier to code.


2 Answers

Indexer is a highly specialized property which allows instances of a class (or struct) to be indexed just like an array (properties can be static but indexers cannot).

Why to use indexers:

  • instead of a new data structure, the class itself is a data structure.
  • simplified syntax - syntactic sugar

When to use:

  • if your class needs list(/array) of its instances (example 1)
  • if your class represents list(/array) of values directly related to your class (example 2)

Example 1:

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 = "Hassan"};
p[1] = new Person(){Name = "John Skeet"};    

Example 2:

class TempratureRecord{
    private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, 61.3F, 56.5F, 56.9F, 58.8F, 61.3F};

    public int Length{
        get { return temps.Length; }
    }

    public float this[int index]
    {
        get{
            return temps[index];
        }
        set{
            temps[index] = value;
        }
    }
}
like image 190
HASSAN MD TAREQ Avatar answered Oct 20 '22 19:10

HASSAN MD TAREQ


Heres a video i have created http://www.youtube.com/watch?v=HdtEQqu0yOY and below is a detailed explanation about the same.

Indexers helps to access contained collection with in a class using a simplified interface. It’s a syntactic sugar.

For instance lets say you have a customer class with addresses collection inside it. Now let’s say we would like to like fetch the addresses collection by “Pincode” and “PhoneNumber”. So the logical step would be that you would go and create two overloaded functions one which fetches by using “PhoneNumber” and the other by “PinCode”. You can see in the below code we have two functions defined.

Customer Customers = new Customer();
Customers.getAddress(1001);
Customers.getAddress("9090");

If you use indexer you can simplify the above code with something as shown in the below code.

Customer Customers = new Customer();
Address o = Customers[10001];
o = Customers["4320948"];

Cheers.

like image 28
Shivprasad Koirala Avatar answered Oct 20 '22 17:10

Shivprasad Koirala