在C#中明确实现接口与实现接口

pro*_*eek 16 c# interface visual-studio-2010 visual-studio

我在VS2010中有两个用于实现接口的选项.

在此输入图像描述

当我有IHelper.cs接口时如下:

public interface IHelper
    ....
    IEnumerable<IPort> Ports { get; }
Run Code Online (Sandbox Code Playgroud)

"显式实现接口"给出了以下代码:

    IEnumerable<IPort> IHelper.Ports
    {
        get
        {
            ...
        }
    }
Run Code Online (Sandbox Code Playgroud)

并且,"实现接口"给了我这个代码:

    public IEnumerable<IPort> Ports
    {
        get
        {
            ...
        }
    }
Run Code Online (Sandbox Code Playgroud)

它们是相同还是不同?为什么我在C#中实现接口有两种选择?

Ada*_*rth 28

显式接口声明意味着接口成员在接口本身以外的类型上不可用,因此在公开访问接口之前,需要将实现类型强制转换为接口.

Implicit是实现大多数接口的标准方式,它在implementsor-type的公共API上公开接口项.

显式接口定义的主要原因是,如果碰巧实现包含具有相同签名的方法的两个接口,则避免命名冲突......显式定义允许编译器保持签名足够清晰以便解析.

正如XenoPuTtSs在评论中所建议的那样,支持代码维护的第二个原因是,如果删除方法签名,显式定义将触发实现类型上的编译器错误.在隐式实现中,从接口中删除方法将使方法成为任何类型的常规成员 - 这意味着您需要手动搜索现在已解散的方法实现.

  • 另外(这不是主要原因)当接口发生变化时(如删除或更改方法),编译器将在明确定义接口的类上抛出错误,因为这些方法不再在接口上.这是一种有用的方法(恕我直言),用于查找大型项目中未使用的代码. (2认同)

Oyb*_*bek 7

他们是完全不同的.如果明确地实现接口,则只能通过引用该接口来引用接口成员.以下代码演示了这个想法.

public interface IFoo {
    String Bar { get; set; }
}
public class ImplicitFoo : IFoo {
    public string Bar {get;set;}
}
public class ExplicitFoo : IFoo {
    private String _Bar;
    string IFoo.Bar {
        get {
            return _Bar;
        }
        set {
            _Bar = value;
        }
    }
}
public class Test {
    public void Test() {
        var iml = new ImplicitFoo();
        // Works fine
        Console.WriteLine(iml.Bar);
        var expl = new ExplicitFoo();
        var fooInterface = (IFoo)expl;
        // Works fine
        Console.WriteLine(fooInterface.Bar);
        // Throws compile time exception
        Console.WriteLine(expl.Bar);
    }
}
Run Code Online (Sandbox Code Playgroud)


Ser*_*hei 5

实现接口的类可以显式实现该接口的成员.显式实现成员时,不能通过类实例访问它,而只能通过接口的实例访问它.

// explicit1.cs
interface IDimensions 
{
  float Length();
  float Width();
}

class Box : IDimensions 
{
  float lengthInches;
  float widthInches;

 public Box(float length, float width) 
 {
    lengthInches = length;
   widthInches = width;
 }
 // Explicit interface member implementation: 
 float IDimensions.Length() 
 {
    return lengthInches;
 }
 // Explicit interface member implementation:
 float IDimensions.Width() 
 {
    return widthInches;      
 }

 public static void Main() 
 {
   // Declare a class instance "myBox":
   Box myBox = new Box(30.0f, 20.0f);
  // Declare an interface instance "myDimensions":
  IDimensions myDimensions = (IDimensions) myBox;
  // Print out the dimensions of the box:
  /* The following commented lines would produce compilation 
     errors because they try to access an explicitly implemented
     interface member from a class instance:                   */
  //System.Console.WriteLine("Length: {0}", myBox.Length());
  //System.Console.WriteLine("Width: {0}", myBox.Width());
  /* Print out the dimensions of the box by calling the methods 
     from an instance of the interface:                         */
  System.Console.WriteLine("Length: {0}", myDimensions.Length());
  System.Console.WriteLine("Width: {0}", myDimensions.Width());
 }
}
Run Code Online (Sandbox Code Playgroud)

显式接口实现还允许程序员继承两个共享相同成员名称的接口,并为每个接口成员提供单独的实现.此示例以公制和英制单位显示框的尺寸.Box类继承了两个接口IEnglishDimensions和IMetricDimensions,它们代表不同的测量系统.两个接口都具有相同的成员名称,长度和宽度.

看一下这个例子

// explicit2.cs
// Declare the English units interface:
interface IEnglishDimensions 
{
  float Length();
  float Width();
}
// Declare the metric units interface:
interface IMetricDimensions 
{
   float Length();
   float Width();
}
// Declare the "Box" class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions 
{
   float lengthInches;
   float widthInches;
 public Box(float length, float width) 
  {
    lengthInches = length;
    widthInches = width;
  }
// Explicitly implement the members of IEnglishDimensions:
float IEnglishDimensions.Length() 
{
  return lengthInches;
}
float IEnglishDimensions.Width() 
{
  return widthInches;      
}
 // Explicitly implement the members of IMetricDimensions:
float IMetricDimensions.Length() 
{
   return lengthInches * 2.54f;
}
float IMetricDimensions.Width() 
{
  return widthInches * 2.54f;
}
public static void Main() 
{
  // Declare a class instance "myBox":
  Box myBox = new Box(30.0f, 20.0f);
  // Declare an instance of the English units interface:
  IEnglishDimensions eDimensions = (IEnglishDimensions) myBox;
  // Declare an instance of the metric units interface:
  IMetricDimensions mDimensions = (IMetricDimensions) myBox;
  // Print dimensions in English units:
  System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
  System.Console.WriteLine("Width (in): {0}", eDimensions.Width());
  // Print dimensions in metric units:
  System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
  System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
 }
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看此文章