Interface Key Note

Interface Key Notes ( MSDN Interface)
1. Explicit Interface Implementation
There are 2: implicit and explicit interface implementation. Implicit type is normal to be used. Here, to understand more about why needs explicit interface implementation, give 2 reasons got from
Unleashed C#:
    -- Hide interface method / event / property / indexer in case to direct use the derived class.
    -- There are same signature functions in the derived class's parent interfaces, so it needs things like IBroker.GetRating and IAdviser.GetRating explicitly to get two implementations.

"Sometimes it’s necessary to explicitly declare which interface a class or struct member implements. One common reason for explicit implementation is when there is multiple interface inheritance and two or more interfaces declare a member with the same name.
Another reason to use explicit interface implementation is to hide a specific implementation.
To perform explicit interface implementation, a class implements an interface member by using its fully qualified name. The implementation is not declared with modifiers, because
they are implicitly hidden from an object of the implementing class. However, they are implicitly visible to objects of the explicit interface type."

2. Interface Mapping .
public class Accountant : StockBroker, IBroker
{
// no implementation
}
If StockBroker's implementation matchs IBroker's requirement, the Accountant class won't require any implementation to get compiler believe it's correct! This is interface mapping in C#!

This is because with C# interface mapping, the implementation of the StockBroker class is used to map to the implementation requirements of the IBroker interface.

3. Can Add Virtual in derived class of the interfaces

Normally, we don't need to add virtual keyword in the derived class of interfaces. But there are cases we need to add virtual to enable the derived class's children can override some behaviors. From MSDN: "It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members."

你可能感兴趣的:(interface)