I know that we can't create instance of Interface.
but why we can write Interface on left side? Are those only references of Interface
(for classes that implement this one) instead instance?
If we can't create instance of Interface, what type is P in this example?
string[] names = {"John", "Bob", "Mark"};
IEnumerable<string> P = names.Where(n => n.Contains('o'));
P is of type IEnumerable<string>
Here you are declaring the type of the local variable P.
P holds the type of IEnumerable<string>, and while you aren't instantiating the interface directly, you can instantiate a concrete class but simply hold a reference to the interface.
The instance returned here by the Where() call needs to only adhere to the contract that IEnumerable<string> defines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With