0

Suppose if i have a class like below and if i want to add some types as an list, then i can do this

    class student
    {
        public string name { get; set; }
    }
    class MainClass
    {
        static void Main()
        {
            var lists = new List<student>();
            lists.Add(new student { name = "foo" });
            lists.Add(new student { name = "mary" });
            lists.Add(new student { name = "jane" });
            lists[1].name = "moo";
        }
    }

now, Indexer also does the same, then why should i use indexer over List

    class TempRecord
    {
        // Array of temperature values
        private float[] temps = new float[10];

        public float this[int index]
        {
            get
            {
                return temps[index];
            }

            set
            {
                temps[index] = value;
            }
        }
    }

    class MainClass
    {
        static void Main()
        {
            TempRecord tempRecord = new TempRecord();
            // Use the indexer's set accessor
            tempRecord[3] = 58.3F;
            tempRecord[5] = 60.1F;
        }
    }

my question is 1. why is indexer needed in the first place when we have List which does the same job 2. is there any clr advantage for using the indexer

Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
  • It's *really* unclear what you're asking. `List` is a type; an indexer is a member of a type. It's like asking why you need properties when the string type exists. – Jon Skeet Dec 07 '17 at 09:26
  • @JonSkeet, as i described above, an indexer is used to store and retrive value using the index. the same is also done with the List. so is List and indexer does the same thing or does the Indexer serves some other purpose as well – Lijin Durairaj Dec 07 '17 at 09:41
  • No, they don't do the same thing at all. One is a type, and one is a syntactic construct. It would be more reasonable to compare `List` and arrays - `List` provides a *dynamically sized* collection. It's worth noting that your two examples are very different - in the first, there's a list *in the main method*. In the second, there's an array within your user-defined type. If you'd provided a "with indexers or not" example which was the *same* code but first using an indexer and then not, that would have been better. – Jon Skeet Dec 07 '17 at 09:44
  • @JonSkeet, what are all the things which i can do with Indexer but not with the List, i guess i can do more thing using the List than using the Indexer, then why is the Indexer even exist in C# – Lijin Durairaj Dec 07 '17 at 10:02
  • Again, you've missed my point. `List` is a type. An indexer is not. You simply can't compare them. It's like asking "What are all the things which I can do with a string but not with the division operator?" – Jon Skeet Dec 07 '17 at 10:07
  • @JonSkeet, thank you for the explanation, i read ur books c# in depth and it is interesting :) – Lijin Durairaj Dec 07 '17 at 12:43

0 Answers0