I am not getting proper answer about Interface, I'm really so confused about interface, because we are directly use the class and methods using of object. than why we unnecessary create Interface and Inherit in Class?
What is the actual use of Interface In .Net MVC? What are advantages and disadvantages of Interface?
-
possible duplicate of [C# interfaces - What's the point?](http://stackoverflow.com/questions/6802573/c-sharp-interfaces-whats-the-point) – Jamie Rees Jul 06 '15 at 08:36
-
1Learn a little about Loose coupling https://en.wikipedia.org/wiki/Loose_coupling . When code is written upon abstraction rather than concrete classes, the system is loosely coupled and hence the definition of underlying classes can be altered without breaking whole system. – Sarvesh Mishra Jul 06 '15 at 10:09
1 Answers
Interfaces are a powerful programming tool because they let you separate the definition of objects from their implementation. Here some points from msdn:
1)Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.
2)Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
3)Interfaces are better in situations in which you do not have to inherit implementation from a base class.
4)Interfaces are useful when you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.
for example: lets say you have a program which need to collect data from different parts of it. Every part is not connected to the other, like: Screen, Printer, Beer Machine, and so on. Each class will impelment IDataCollector
and it's functions and eventually you'll be able to do something like that in some manager class:
foreach (ProgramFeature feature in ProgramFeatures)
{
if (feature is IDataCollector)
{
feature.CollectData();
}
}

- 1,768
- 1
- 16
- 24
-
2yes i know uses of interface according https://msdn.microsoft.com/en-us/library/3b5b8ezk(v=vs.90).aspx but can you give me Example we can not don without interface ?? give me a that type of situation we can not done without using interface ?? – Sandip_JACK Jul 06 '15 at 07:23
-
-
2Lets say you created a server (through wcf, webApi and etc.), you want to get some classes and do some jobs. you need a "contract" that we'll give you information of what functions you can activate. You computer doesn't have to know each class potentialy to come - instead you have this "contract" and know the relevant functions you need and can activate and do the job. hope it helps :-) – israel altar Aug 10 '15 at 11:45
-
@israelaltar, have a query here. In ASP.Net MVC as you said I need to implement a functionality that should process the received data to be placed either in _IoTHub_ or _local DB SQLServer_ ( it will be later extended to StorageTable,...) .___So here I decide to create an interface for IoTHub and another for local DB SQLServer. **My Query** Where should I define the _Interface_ ===> In **MODELS**? or **REPOSITORY**? (REPOSITORY is a folder along with Model & View & Controller where I have all the business logic done for the **CONTROLLER** to be called & used) – jAntoni Nov 28 '17 at 07:32