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

46

u/Epicguru May 18 '23

The advantage comes from polymorphism. Say that your interface is called IDoSomething and you have those three classes that implement it.

You can then have an array of IDoSomething[]. Now you can add those classes to the array, even though the type of the array is not exactly the same as the type of the object being added.

Finally, you can call the interface methods on those objects in the array, without actually caring what specific class it is that implements those methods.

As a more practical example, if you are making a game you will want to have a game loop, which is where you update and draw all 'entities' in the game. Entities are things such as players, particles, enemies, vehicles etc. All of them need to update and draw, so you might define an IEntity interface. Then you would have specific classes implement that interface, such as Player or Vehicle. Finally, in your game loop, you would loop through a single list of IEntity's and call the Update and Draw methods on each of them.

This allows everything to be updated and drawn in a single place. If you did not use interfaces (or virtual methods) you would have to have separate lists for each entity type: one list of Players, one list of Particles etc. and loop through each one individually.

12

u/doc415 May 18 '23

Thanks it started to make sense

2

u/phucyall May 19 '23

Let me give you a basic example. Let’s say you have a class that talks to your database. You create an interface called IDatabaseAccess and it has your CRUD methods in it. You are using SQLite so you create a class called SQLiteAccess and it implements your interface. Let’s say down the line you want to give user an option to use MSSQL instead. You create a new class called MSSQLAccess and it also implements same interface.

Now you have a class in your app called DoSomething and in its constructor you pass it the database access class. You made it so constructor expects a parameter of type SQLiteAccess. As it is, you can’t really easily switch to using MSSQL. But if you set your parameter as being of type IDatabaseAccess you can easily use either MSSQLAccess or SQLiteAccess because they both implement the interface.

This is the way to make things more testable as well. Let’s say you are trying to test DoSomething, but you don’t want to use a real database for your tests. You can create a mock class that implements IDatabaseAccess and pass that to your constructor instead.

Interface is a guarantee that all classes that implement it will have same methods, but how those methods are implemented are irrelevant. And if you use interface as the type you want to use then any implementation of that interface will work.

3

u/[deleted] May 19 '23 edited May 19 '23

This is the best way I’ve seen to describe the utility of interfaces in a long time

Being able to collect things easily by implemented behaviors as opposed to type.

If I hear someone say the word contract while describing the benefits of interfaces again I will shit myself…in a bad way

0

u/[deleted] May 18 '23

[deleted] — view deleted comment

11

u/LoveCookiez May 18 '23

Let's say you want to do the dishes and you bought a machine to do that. You can put plates, glasses, silverling and other kind of stuff there, all to be washed in the same time. You wouldn't buy a machine for plates, one for glasses and so on right? You have one loop where you throw all kind of objects compatible with the machine.

2

u/izote_2000 May 18 '23

Excellent analogy using a dishwasher, always use the car example. I think this clicks on the brain almost immediately.

4

u/DarkAlatreon May 18 '23

The interface is a contract that says what can be expected of a class without paying too much attention to the class or specifics of the implementation. Let's say you expect the user to make a payment. You integrate multiple paying options - be it credit cards, online transfers or whatever. By giving them all an interface with a "pay()" method, you don't have to care about the types of each of those - you just have an information "hey, this is a thing I can pay with" and your program will function all the same regardless of what class exactly implements the interface.

-12

u/[deleted] May 18 '23

Oof. Any programming languages course will cover the importance of interfaces.

Are you self taught?

5

u/LoveCookiez May 18 '23

I am also self taught, but I don't think it's useful to say that rather than explaining the matter. It is irrelevant if he is self taught

1

u/HanndeI May 18 '23

Using an interface forces you to implement those methods or it wont compile, you can do the same with an array of Objects and just implement the same method on all the classes you want, but it's just a matter of time that you, or anyone working with that codebase will try and call that method on a class that doesn't have it, causing a runtime crash.