Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

IReadOnlyList - why not ISupportsRandomAccess IHasSize (IModifiable for non-readonly) instead?

Back to Forum: Tech Off
  • User profile image
    androidi

    And then there's stuff like this comment

    "Why do you need to know how many items are in your enumeration in advance?  If you do, you're doing it wrong."

    But how often are you really using say LINQ over "OnlyEnumerable" (as in something that even theoretically can't support random access). Last I checked, most people have RAM (random access memory, FYI) in the computer, so it's really hard to understand why eg. Skip(x) or Reverse() would need to iterate over each element when most of the time the underlying object is something that could implement various interfaces that allow optimizing the access.

     

     

     

  • User profile image
    wkempf

    Huh? I was confused for a bit. Didn't realize we were finally getting IReadOnlyList. Where did your quoted comment come from?

    The design of interfaces in this area can be a bit tricky (but is long overdue, IMHO). To shoot down some of your suggestions: ISupportsRandomAccess would have to be identical to IReadOnlyList. You can't safely index (random access) a collection without knowing the size. If you include the count and an indexer it means you can enumerate the object, so leaving out IEnumerable here would be a mistake. That's why I say ISupportsRandomAccess would have to be identical to IReadOnlyList, and IMHO IReadOnlyList is a far better name. IHasSize, aside from being a horrible name, doesn't sound like something that would be all that useful. Not to mention there's some confusion involved in such an interface. A rectangle has size, for instance, but your IHasSize interface wouldn't be appropriate to use there. As for IModifiable, that's a meaningless name. I assume you meant something like an IModifiableList and IModifiableCollection instead. If the BCL were being written from scratch that would be a bit of a tomayto tomahto argument (though I think IList and IReadOnlyList make a lot more sense), but they aren't starting from scratch. The modifiable variant is already named IList.

  • User profile image
    JohnAskew
  • User profile image
    wkempf

    Thanks for the link. I think most of the questions @androidi posed were answered in that forum thread. The biggest reason to include IReadOnly* interfaces is simply for documentation reasons. Returning an IList<T> with IsReadOnly == false works, but doesn't document the actual expectations. Beyond that, though, there are more important implementation reasons for these types to exist, including variance support and certain optimization patterns you can get in LINQ and other scenarios. Like I said, I'm very glad we're finally getting these types. It's unfortunate that because we're getting them late IList<T> is not an IReadOnlyList<T>.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.