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
Consider the following method as an example: List<int> Foo() { // ... } Are there any disadvantages of returning an interface instead of a concrete implementation in C#? IList<i...
#2: Post edited
- Consider the following method as an example:
- ```cs
- List<int> Foo() {
- // ...
- }
- ```
- Are there any disadvantages of returning an interface instead of a concrete implementation in C#?
- ```cs
List<int> Foo() {- // ...
- }
- ```
- From the caller's perspective, it doesn't really matter what the type is as long as it has the correct methods and behaviors (That's the point of interfaces, after all). Taken another way, if I in the future want to change what I return[^1], the `IList` implementation seems superior in that I don't have to change the return type (as long as my new return value also implements `IList`). Why then would I prefer to return by concrete type?
- [^1]: Setting aside considerations such as API changes; you can assume this is an internal method not consumed by anyone else.
- Consider the following method as an example:
- ```cs
- List<int> Foo() {
- // ...
- }
- ```
- Are there any disadvantages of returning an interface instead of a concrete implementation in C#?
- ```cs
- IList<int> Foo() {
- // ...
- }
- ```
- From the caller's perspective, it doesn't really matter what the type is as long as it has the correct methods and behaviors (That's the point of interfaces, after all). Taken another way, if I in the future want to change what I return[^1], the `IList` implementation seems superior in that I don't have to change the return type (as long as my new return value also implements `IList`). Why then would I prefer to return by concrete type?
- [^1]: Setting aside considerations such as API changes; you can assume this is an internal method not consumed by anyone else.
#1: Initial revision
When would one not want to return an interface?
Consider the following method as an example: ```cs List<int> Foo() { // ... } ``` Are there any disadvantages of returning an interface instead of a concrete implementation in C#? ```cs List<int> Foo() { // ... } ``` From the caller's perspective, it doesn't really matter what the type is as long as it has the correct methods and behaviors (That's the point of interfaces, after all). Taken another way, if I in the future want to change what I return[^1], the `IList` implementation seems superior in that I don't have to change the return type (as long as my new return value also implements `IList`). Why then would I prefer to return by concrete type? [^1]: Setting aside considerations such as API changes; you can assume this is an internal method not consumed by anyone else.