Benefits of implementing an interface

后端 未结 10 1087
小鲜肉
小鲜肉 2020-12-06 04:58

what are the benefits of implementing an interface in C# 3.5 ?

相关标签:
10条回答
  • 2020-12-06 05:39

    An Interface is a reference type and it contains only abstract members. Interface's members can be Events, Methods, Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can't contain constants, data fields, constructors, destructors and static members. All the member declarations inside interface are implicitly public.

    0 讨论(0)
  • 2020-12-06 05:44

    The main benefit is about code readability, code maintainability and code "semantics".

    • Code readability: An interface constitutes a declaration about intentions. It defines a capability of your class, what your class is capable of doing. If you implement ISortable you're clearly stating that your class can be sorted, same for IRenderable or IConvertible.
    • Code semantics: By providing interfaces and implementing them you're actively separating concepts in a similar way HTML and CSS does. A class is a concrete implementation of an "object class" some way of representing the reality by modeling general properties of real life objects or concepts. An interface define a behavioral model, a definition of what an object can do. Separating those concepts keeps the semantics of your code more clear. That way some methods may need an instance of an animal class while other may accept whatever object you throw at them as long as it supports "walking".
    • Code maintainability: Interfaces helps to reduce coupling and therefore allow you to easily interchange implementations for the same concept without the underlying code being affected. You can change the implementation of a IMessage easily by defining a new class that implements the interface. Compare that to sistematically replacing all references from CMessage to CMyNewMessageClass.
    0 讨论(0)
  • 2020-12-06 05:45

    The main benefits of interfaces is mostly related to project design.

    If you use an interface:

    1. The consumer of the interface should implement that interface.
    2. Designing bridge patters.
    3. Creating a contract so that user must adhere the rules of the interface.
    4. Can take only interface part (Object) from the main class.
    5. Even class is private, can obtain the interface object from that
    6. Multiple inheritance kind of style.
    7. Need not be should implement, simple go for if implements that means if you want you can implement other wise can drop it..
    8. Cleaner code.
    9. Implementation which changes depends on class can go ahead with interface.
    10. If each class have separate implementation of a method better to go for interfaces. For example IEnumerable in collections.

    According to C# Architect, in a simple word it's a contract. Consumer must adhere to it.

    0 讨论(0)
  • 2020-12-06 05:51

    An interface defines a contract (things that an object is able to do), while a concrete class (or struct) defines the concrete behavior.

    For an example, IList is an interface, it defines the methods that a concrete object has to provide in order to be used like any other object implementing IList. Everywhere an IList can be used, your object that implements IList can be used as well. The way you concretely implement it and the way your object behaves when those IList methods are called is left to you.

    0 讨论(0)
  • 2020-12-06 05:55

    Interfaces provide no actual advantage. Anything that can be done with an interface can, and should be done using other language constructions. Multiple inheritance is oft quoted as the only REAL benefit derived from using interfaces, but I can do multiple inheritance quite easily and clearly in C# - I do it every day. Changing the code without "breaking" the interface is the silliest of all excuses... That applies the same to concrete classes as it does to abstract classes or interfaces. As long as the functional signature does not change, you haven't broken the interface. Doesn't matter where it was declared. Simply putting a functional prototype in a separate file and naming it with an "I" in front buys nothing - except that you end up with twice as many source files to maintain. The supposition that the interface is defined early, and then maintains the contract is ridiculous. Interface methods and their parameters change ALL the time, because everything is never known up-front. That's why MicroSof stopped using them long ago. They had IUnKnown, IUnknown2, etc. It created a mess.

    0 讨论(0)
  • 2020-12-06 05:58

    You'll be able to pass your object to a method (or satisfy a type constraint) that expects the interface as an argument. C# does not support "duck typing." Just by writing the methods defined by the interface, the object will not automatically be "compatible" with the interface type:

    public void PrintCollection<T>(IEnumerable<T> collection) {
        foreach (var x in collection)
           Console.WriteLine(x);
    }
    

    If List<T> did not implement the IEnumerable<T> interface, you wouldn't be able to pass it as an argument to PrintCollection method (even if it had a GetEnumerator method).

    Basically, an interface declares a contract. Implementing an interface enforces your class to be bound to the contract (by providing the appropriate members). Consequently, everything that relies on that contract (a method that relies on the functionality specified by the interface to be provided by your object) can work with your object too.

    0 讨论(0)
提交回复
热议问题