Recently, I have found out that indexer can accept an array of arguments as params
:
public class SuperDictionary<TKey, TValue>
{
public Dictionary<TKey, TValue> Dict { get; } = new Dictionary<TKey, TValue>();
public IEnumerable<TValue> this[params TKey[] keys]
{
get { return keys.Select(key => Dict[key]); }
}
}
Then, you will be able to do:
var sd = new SuperDictionary<string, object>();
/* Add values */
var res = sd["a", "b"];
However, I never met such usage in .NET Framework or any third-party libraries. Why has it been implemented? What is the practical usage of being able to introduce params
indexer?
The answer has been found in a minute after posting the question and looking through the code and documentation - C# allows you to use any type as a parameter for indexer, but not
params
as a special case.According to MSDN,
In other words, indexer can be of any type. It can either be an array...
or any kind of another unusual type or collection, including
params
array, if it seems to be convenient and suitable in your case.