C# interface implementation: the best choice of hidden and explicit methods
The interface definition in C# is defined as a contract to provide a contract. These contracts can be implemented in hidden or explicitly, each with its own characteristics.
hidden implementation
This method defines the interface members as conventional members who implement class. This simplifies the code and allows to directly access the interface through the class itself, both of which can be transformed. For example:
Expressing
<code class="language-csharp">class MyClass : IList { public void CopyTo(Array array, int index) { // CopyTo 的实现 } }</code>
In the explicit implementation, the definition of the interface members includes the interface name. This limits can only access the interface by compulsory converting to the interface itself:
advantages and disadvantages
<code class="language-csharp">class MyClass : IList { void IList.CopyTo(Array array, int index) { // CopyTo 的实现 } }</code>
The code is more concise
You can access the interface through class and interfaces
The access to the interface is limited
The above is the detailed content of C#: Implicit vs. Explicit Interface Implementation: Which Approach is Best?. For more information, please follow other related articles on the PHP Chinese website!