0

When should I create an interface?

In general, when should I create an interface? Why not just create a class library and implement it's methods where I need them? What benefits does an interface serve?

3rd Apr 2017, 10:23 PM
Mike
Mike - avatar
2 odpowiedzi
+ 21
"By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes. In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class." - MSDN Your class can inherit behavior from different sources, but interface has no implementation of methods, so you must do that in your class (signature must be the same as interface members` one). That way we`re kinda free to go with our own method implementations, which is very useful thing.
3rd Apr 2017, 11:02 PM
Illusive Man
Illusive Man - avatar
+ 3
Here is the live Example @Michael Suppose your parents gives you a list of items to purchase, that list is an Interface that you will implement at time of purchasing..... Means Before implementing any thing , you list out what you have to done that will be interface. I hope it will help you Thanks
10th Apr 2017, 9:17 AM
Aditya Gupta
Aditya Gupta - avatar