I have implemented the [ ] operator in a class that I have created to return values associated with a key (the same concept of a dictionary, except that it's possible to have the same key more than one time). However, looking at my code I don't think it is as efficient as it could be.public class MyClass{    public object[][] Values;    public MyClass()    {        Values = new object[4][];    }    public IEnumerable<object> this[string key]    {        get        {            var results = new List<object>();            foreach (var value in this.Values)            {                if (value[0].ToString() == key)                {                     results.Add(value[1]);                }             }            return results;        }    }}Values can contain a bunch of 2 values arrays.A small example:MyClass class = new MyClass();class.Values[0] = new object[] { key1, 25 };class.Values[1] = new object[] { key2, 3 };class.Values[2] = new object[] { key2, 789 };class.Values[3] = new object[] { key4, 12 };and when I do class[key2], it returns a collection containing 3 and 789.What do you think of the way I implemented the this[string key] method? Do you think there is a way to do this without iterating on all the objects in the Values array?
Implementing [ ] operator
c#;collections
If you look for performance, you'd better rely on the framework dictionary. You can use the Dictionary<string, List<object>> to do what you need. When you add a new key and value, look if the key already exists. If so, add the value to the array and don't add the key.