Home >Backend Development >C++ >Implicit vs. Explicit Interface Implementation in C#: When Should I Use Which?

Implicit vs. Explicit Interface Implementation in C#: When Should I Use Which?

Barbara Streisand
Barbara StreisandOriginal
2025-02-01 08:41:09576browse

Implicit vs. Explicit Interface Implementation in C#: When Should I Use Which?

C# interface implementation: hidden and explicit method

The interface in C# defines the contract that the class or the structure must follow. When implementing the interface, you can choose two methods: hidden implementation and explicit implementation.

hidden implementation

Rectify the interface method as part of a class or structure. These methods can be accessed in two ways: class and interface. Examples as follows:

Expressing

<code class="language-csharp">public interface ICopyable
{
    void CopyTo(Array array, int index);
}

public class MyClass : ICopyable
{
    public void CopyTo(Array array, int index)
    {
        // 实现代码
    }
}</code>
Expressing the interface method separately in the class or structure. These methods can only be accessed through interfaces and cannot be accessed through class. Examples as follows:

When to use hidden or explicit implementation

<code class="language-csharp">public interface ICopyable
{
    void CopyTo(Array array, int index);
}

public class MyClass : ICopyable
{
    void ICopyable.CopyTo(Array array, int index)
    {
        // 实现代码
    }
}</code>
When the implementation of a class or structure is completely consistent with the definition in the interface,

hidden implementation . It simplifies the code by eliminating the needs of the definition of explicit method.

  • The following situations should be considered

    Expressing :

  • Need to rewrite the default implementation provided by the interface.
  • You need to realize multiple interfaces with the same method name but implement different. It needs to be implemented from a hidden interface from a public API of a class or structure.

    • advantages and disadvantages
    • Expressing implementation:

Advantages: Allows more detailed control to implement details, and help avoids ambiguousness when defining conflict methods in multiple interfaces.

Disadvantages:

The code may lead to lengthy and complicated.

Into the implementation:
  • Advantages:
  • The code is simpler and the number of code rows is less.
  • Disadvantages: less control over the implementation of details.

Microsoft's guidance principles on explicit implementation

Microsoft's guidance principles are usually recommended to avoid explicit implementation because it may lead to accidental behavior. However, the principle of this guidance may be previously formulated before dependency injection (DI). When using DI, interfaces are usually passed. In this case, it may be more beneficial to explicit implementation.
  • In short, the choice of hidden and explicit interface implementation depends on the specific needs of the code. Cisectional implementation usually provides more concise solutions, and explicitly implement greater flexibility and control of interface implementation.

The above is the detailed content of Implicit vs. Explicit Interface Implementation in C#: When Should I Use Which?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn