What C# interface should be used if I only want to be able to index into instances of a type? I don’t need (or want) the ability to add/remove/edit elements. Enumeration is okay. Does this require a custom IIndexable type?
In this case IList is overkill because it forces implementation of members I don’t want to have.
(Edit: This question was asked before .NET 4.5, which now includes the more suitable
IReadOnlyList<>interface. The following Answer is therefore valid for .NET before 4.5.)IList<>(assuming you want to stay generic) is the only interface to include an indexer in .NET before version 4.5.However, you can just explicitly implement and throw
NotSupportedExceptionfor all those operations you don’t want to support, or just implementIEnumerable<>and have the rest on the class only, not in an interface.