Programing

암시 적 인터페이스와 명시 적 인터페이스 구현

crosscheck 2020. 10. 24. 09:37
반응형

암시 적 인터페이스와 명시 적 인터페이스 구현


중복 가능성 :
C # : 인터페이스-암시 적 및 명시 적 구현

누군가이 두 짐승의 차이점과 사용 방법을 설명 하시겠습니까? AFAIK, 많은 pre.2.0 클래스가 제네릭 유형없이 구현되었으므로 후자 버전에서 두 가지 인터페이스를 모두 구현했습니다. 그것들을 사용해야하는 유일한 경우입니까?

사용법도 자세히 설명해 주시겠습니까?

감사


이것에 대한 훌륭하고 매우 상세한 블로그 게시물이 있습니다.

기본적으로 암시 적 인터페이스 구현을 사용하면 마치 클래스의 일부인 것처럼 인터페이스 메서드와 속성에 액세스 할 수 있습니다. 명시 적 인터페이스 구현을 사용하면 해당 인터페이스로 취급 할 때만 액세스 할 수 있습니다.

다른 하나를 사용하는 경우와 관련하여 인터페이스와 동일한 서명을 가진 속성 / 메서드가 있거나 동일한 서명을 가진 두 개의 인터페이스를 구현하고 다른 구현을 원하므로 명시 적 인터페이스 구현을 사용해야합니다. 일치하는 속성 / 방법.

아래 규칙은 Brad Abrams 디자인 지침 블로그 에서 가져온 것 입니다.

  • 명시 적 멤버를 보안 경계로 사용 하지 마십시오 . 인스턴스를 인터페이스로 캐스트하는 모든 클라이언트가 호출 할 수 있습니다.
  • 음주 숨기기 구현 세부 사항에 명시 적으로 멤버를 사용
  • 음주 대략적인 개인 인터페이스의 구현에 명시 적으로 멤버를 사용합니다.
  • 액세스 서브 클래스가 오버라이드 (override) 할 수 있는지 어떤 명시 적으로 구현 구성원을 다른 방법을 노출합니다. 충돌이 발생하지 않는 한 동일한 메소드 이름을 사용하십시오.

또한 값 유형에 대한 명시 적 구현을 ​​사용할 때 권투가 포함된다는 Brad의 블로그 댓글에도 언급되어 있으므로 성능 비용에 유의하십시오.


평신도의 용어로 클래스가 2 개 이상의 인터페이스에서 상속되고 인터페이스가 동일한 메서드 이름을 갖는 경우 암시 적 인터페이스 구현을 사용하는 경우 클래스가 구현되는 인터페이스 메서드를 알지 못합니다. 이것은 인터페이스를 명시 적으로 구현하는 시나리오 중 하나입니다.

암시 적 인터페이스 구현

public class MyClass : InterfaceOne, InterfaceTwo
{
    public void InterfaceMethod()
    {
        Console.WriteLine("Which interface method is this?");
    }
}

interface InterfaceOne
{
    void InterfaceMethod();
}

interface InterfaceTwo
{
    void InterfaceMethod();
}

명시 적 인터페이스 구현

public class MyClass : InterfaceOne, InterfaceTwo
{
    void InterfaceOne.InterfaceMethod()
    {
        Console.WriteLine("Which interface method is this?");
    }

    void InterfaceTwo.InterfaceMethod()
    {
        Console.WriteLine("Which interface method is this?");
    }
}

interface InterfaceOne
{
    void InterfaceMethod();
}

interface InterfaceTwo
{
    void InterfaceMethod();
}

The following link has an excellent video explaining this concept
Explicit Interface Implementation


There is one more way to look at it, from the labyrinthine implementation itself, here: http://blogs.msdn.com/cbrumme/archive/2003/05/03/51381.aspx.

But in short, implicit implementation gives you an is-a type conversion, explicit implementation won't be accessible unless the object is explicitly type cast to that interface type.

참고URL : https://stackoverflow.com/questions/598714/implicit-vs-explicit-interface-implementation

반응형