Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A When would one not want to return an interface?

IList<T> is not necessarily representative of the general case; it's an interface that is (A) widely implemented by a variety of classes from a variety of sources, which themselves (B) tend t...

posted 2y ago by r~~‭  ·  edited 2y ago by r~~‭

Answer
#2: Post edited by user avatar r~~‭ · 2023-01-09T16:37:35Z (almost 2 years ago)
  • `IList<T>` is not necessarily representative of the general case; it's an interface that is (A) widely implemented by a variety of classes from a variety of sources, which themselves (B) tend to add additional functionality or constraints not captured by the signature of `IList<T>`. There is, in my opinion, very little if any reason to ever return a `List<T>` instead of an `IList<T>`, but you could very well want to return a `SortedSet<T>` in order to get at its `Min` and `Max` properties.
  • Ideally, for that example, there'd be some sort of `ISortedSet<T>` interface that would abstract over those properties. But there isn't, as of .NET 7.0, which historically is representative of the story with these abstract collection interfaces. Good ideas, 80% execution. So in practice, sometimes you want to type things concretely.
  • For interfaces that don't get around as much, like ye olde `IMyInternalApplicationService`, you won't go wrong always favoring those over their concrete implementations everywhere in your code except the place where you tie off your dependency injection knots.
  • `IList<T>` is not necessarily representative of the general case; it's an interface that is (A) widely implemented by a variety of classes from a variety of sources, which themselves (B) tend to add additional functionality or constraints not captured by the signature of `IList<T>`. There is, in my opinion, very little <s>if any</s> [see below for one] reason to ever return a `List<T>` instead of an `IList<T>`, but you could very well want to return a `SortedSet<T>` in order to get at its `Min` and `Max` properties.
  • Ideally, for that example, there'd be some sort of `ISortedSet<T>` interface that would abstract over those properties. But there isn't, as of .NET 7.0, which historically is representative of the story with these abstract collection interfaces. Good ideas, 80% execution. So in practice, sometimes you want to type things concretely.
  • ---
  • Peter Taylor adds: ‘Another issue specific to `IList<T>` is that for legacy reasons it doesn't inherit from `IReadOnlyList<T>`. Returning `List<T>` allows callers to assign to `IReadOnlyList<T>`, which can make it easier to reason about the calling code.’
  • ---
  • For interfaces that don't get around as much, like ye olde `IMyInternalApplicationService`, you won't go wrong always favoring those over their concrete implementations everywhere in your code except the place where you tie off your dependency injection knots.
#1: Initial revision by user avatar r~~‭ · 2022-12-22T00:09:38Z (almost 2 years ago)
`IList<T>` is not necessarily representative of the general case; it's an interface that is (A) widely implemented by a variety of classes from a variety of sources, which themselves (B) tend to add additional functionality or constraints not captured by the signature of `IList<T>`. There is, in my opinion, very little if any reason to ever return a `List<T>` instead of an `IList<T>`, but you could very well want to return a `SortedSet<T>` in order to get at its `Min` and `Max` properties.

Ideally, for that example, there'd be some sort of `ISortedSet<T>` interface that would abstract over those properties. But there isn't, as of .NET 7.0, which historically is representative of the story with these abstract collection interfaces. Good ideas, 80% execution. So in practice, sometimes you want to type things concretely.

For interfaces that don't get around as much, like ye olde `IMyInternalApplicationService`, you won't go wrong always favoring those over their concrete implementations everywhere in your code except the place where you tie off your dependency injection knots.