ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

why use interface instead of creating a class with implemented methods & instantiating & using it?

user-image
تم إضافة السؤال من قبل Anees Mohammed Niamathullah , .NET Technical Lead , Cognizant Technology Solutions
تاريخ النشر: 2013/06/06
Hussein Fareed
من قبل Hussein Fareed , BI and Data Warehouse Supervisor , TE Data

Interfaces are mostly used to define a capability of a class in order to behave consistently.
For example, a class may inherit an interface (IEnumerable) which specifies that this class can be used in a foreach loop.
As mentioned above in Nahjmal's answer, one of the main distinctions between interfaces and classes is the ability of multiple inheritance.
Multiple inheritance is prohibited in C# using classes unlike C++ so interfaces gives you the ability to simulate this ability.

Mohammed Rizwan Afgani
من قبل Mohammed Rizwan Afgani , Software Engineer, Team Lead , Prosoft e-Solutions (I) Pvt Ltd

Interface helps to design loosely coupled architecture.
It allows us to connect with different implementation of class, in different environment (i.e., Test, Development, Production).
If we directly use class instant.
Design gets rigid for further extending of the application, etc.

Najmal Karuthedath
من قبل Najmal Karuthedath , Dot Net Developer , Dicetek

Interface is a contract that defines the signature of the functionality.
So if a class is implementing a interface it says to the outer world, that it provides specific behavior.
Example if a class is implementing Idisposable interface that means it has a functionality to release unmanaged resources.
Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.
Interface helps to implement multiple Inheritance in .NET framework.

karim kamel
من قبل karim kamel , Developer Support Engineer , Microsoft

Purposes of Interfaces create loosely coupled software support design by contract (an implementor must provide the entire interface) allow for pluggable software allow different objects to interact easily hide implementation details of classes from each other facilitate reuse of software

المزيد من الأسئلة المماثلة