3

I know it is very general question but I think it would be very beneficial for less experienced programmers to get clear answer.

Scenario:

Let say I have MyClass which is implementing interfaces IMembers1, IMembers2, IMembers3. MyClass also contains members which are not defined in any of those interfaces.

Question:

Is there any benefit in using f.e. IMembers2 reference to access IMembers2 members in MyClass instance instead accessing them by simply using MyClass reference.

If I have instance of type which is unknown at compile time but I'm sure it will implement IMembers2 interface I would of course use IMembers2 reference to access its members. Lots of code examples and tutorials are teaching to use f.e. IEnumerable<T> reference to access list collection in heap instead of using List<T> reference.

cembo
  • 1,039
  • 2
  • 9
  • 18
  • Possible duplicate of [Interface vs Base class](https://stackoverflow.com/questions/56867/interface-vs-base-class) –  Apr 17 '18 at 04:51
  • It depends. Will all classes always implement IMembers1, IMembers2, and IMembers3? If not, then it would be more flexible for methods to accept the interface they need as an argument. – ProgrammingLlama Apr 17 '18 at 04:54
  • Quote: "Lots of code examples and tutorials are teaching to use f.e. IEnumerable reference to access list collection in heap instead of using List reference." You should downgrade your opinion of things which teach that for [this reason](https://stackoverflow.com/a/8240935/5198140). – Richardissimo Apr 17 '18 at 05:41

0 Answers0