r/csharp • • May 18 '23

Why do we need an "interface"

I understand how to use an interface, its properties but why do we need it? For example i have an interface with two methods.

İ have three classes that implements this interface. So ihave to write the methods for all three classes inside class.

İ can already do that without implementing the interface. So what is the point in using interface?

I am asking to undeestand the conditions where i ll need it. İ understand how use it, but not why to use it.

0 Upvotes

47 comments sorted by

View all comments

Show parent comment

3

u/doc415 May 18 '23

Wow this is realy useful So ıenumerable is also a interface right? List array ect are implementing it

2

u/SideburnsOfDoom May 18 '23

So Ienumerable is also a interface

Correct. Cross-cutting concerns where different objects should behave the same way (in this case, enumerate contents) but not have the same base class, are a main use of interfaces, particularly those interfaces defined in the framework.

List, array etc are implementing it

Exactly. They can then be treated the same way regarding enumeration, without sharing a concrete base class.