-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
The compiler does not bind the extension method ServiceCollectionDescriptorExtensions.Add(this IServiceCollection collection, ServiceDescriptor descriptor) when the method is called on an instance of type IServiceCollection. This is the correct compiler behavior, since IServiceCollection derives from IList<ServiceDescriptor>, and IList<> has an interface definition for Add.
On the other hand, when calling Add on an instance of type ServiceCollection (the concrete class, not the interface), the compiler binds the extension method. This is due to the fact that ServiceCollection uses explicit interface method implementation for the Add method.
This is correct so far, but I wonder what is the point for the extension method?
- It is not bound on
IServiceCollectioninstances. - It is bound on
ServiceCollectioninstances. - It adds a null-check, which is missing in the implemention of
ServiceCollection.
Wouldn't it be better to put the null-check into the class implementation?
An alternative is to rename the extension method to something like AddDescriptor or AddService.