Link to home
Start Free TrialLog in
Avatar of Babak Sekandari
Babak SekandariFlag for United States of America

asked on

What's the benefit of a C# indexer ?

What advantage do C# indexers provide that are not present in other data structures?

From the indexer examples I've seen, I could do the same thing with List<> or HashTable or Dictionary or some other data structure. 

Since I doubt that they would arbitrarily add another data structure for no reason, I wonder what I'm missing.

Thanks

Avatar of ste5an
ste5an
Flag of Germany image

None. They are only syntactic sugar to simulate an array access.

In 99% of the cases, the existing list-type structures will work for you. The last time I implemented an indexer was back when I was learning how to do it, thinking I would need to do it at some point. 


Many many years later, I still haven't found a need for implementing an indexer on a custom class. 


I think the only time I would ever need to do it is if I needed to have some kind of custom logic behind the traversal (e.g. an indexer that traversed an array in a Fibonacci sequence so going to the "next" item actually skipped over some items). 

ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Babak Sekandari

ASKER

Hi Kyle,
The link you provided stated,
...access items using a numerical value or string value...
Then it gave this example,
users["julie"] = new User("Julie Lerman", "joelin@indo.com");
users["mark"] = new User("Mark Lettuce", "mark@lettuce.com");
users[3] = new User("Peter Mbanugo", "p.mbanugo@yahoo.com");
users[3] = new User("Peter Liko", "liko@jordan.com");

Open in new window

I guess that's the benefit of indexers.
Unless there is an existing C# data structure which does that and that I don't know about, yours is the correct answer.

Here is why I asked this question. I saw an example using indexers here,
dependency injection with indexer in the model
which seemed like it could have been done using Dictionary or something else.
public class Repository : IRepository
    {
        private Dictionary<string, Product> products;
        public Repository()
        {
            products = new Dictionary<string, Product>();
            new List<Product> {
                new Product { Name = "Women Shoes", Price = 99M },
                new Product { Name = "Skirts", Price = 29.99M },
                new Product { Name = "Pants", Price = 40.5M }
            }.ForEach(p => AddProduct(p));
        }
  
        public IEnumerable<Product> Products => products.Values;
        public Product this[string name] => products[name];
        public void AddProduct(Product product) => products[product.Name] = product;
        public void DeleteProduct(Product product) => products.Remove(product.Name);
    }

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial