Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface on left side

Tags:

c#

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'));
like image 991
Boolor Avatar asked Jun 21 '26 02:06

Boolor


1 Answers

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.

like image 179
Simon Avatar answered Jun 22 '26 15:06

Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!