Home >Backend Development >C++ >List vs. IList: When Should You Choose the Interface Over the Concrete Class?
List
and iList choice
often confusing. In order to understand their differences, let's explore why List<T>
is better than IList<T>
in some cases. IList<T>
List<T>
Why choose iList
?
Example: IList<T>
List<T>
. If you use to implement this method, follow -up changes (such as replacing it with a custom set) will require library users to update its code. In contrast, the use of to ensure that the interface remains unchanged, and it can maintain compatibility no matter how the underlying implementation. Internal use:
IList<int>
However, these considerations are not so important for internal use cases. In this case, using is enough, because changes to implementation will not affect external users. List<int>
IList<int>
In short, the choice of
depends on the specific context and needs of the application. For the design of the library, is used to maintain flexibility and compatibility, and is suitable for internal use, which is not important to achieve details.
The above is the detailed content of List vs. IList: When Should You Choose the Interface Over the Concrete Class?. For more information, please follow other related articles on the PHP Chinese website!