C# 显式实现不需要';我的情况不行

C# 显式实现不需要';我的情况不行,c#,interface,C#,Interface,我不太明白显式实现是如何工作的,所以我做了一个模型 public class ShoppingCart : IEnumerable<Product> { public IEnumerator<Product> GetEnumerator() { return Products.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() //why 'public' is not

我不太明白显式实现是如何工作的,所以我做了一个模型

public class ShoppingCart : IEnumerable<Product>
{
   public IEnumerator<Product> GetEnumerator()
   {
      return Products.GetEnumerator(); 
   }
   IEnumerator IEnumerable.GetEnumerator() //why 'public' is not needed?
   {
      return GetEnumerator(); // why there is not any error here?
   }
}
但是我有一个编译错误,但是为什么ShoppingCart没有错误呢


我怎样才能让我的模拟课起作用呢?

让我一个接一个地回答问题:

为什么不需要“公共”呢

因为如果我们把
公诸于众
我们会有一个歧义:

到目前为止,对于
ShoppingCart.GetEnumerator()的情况,效果良好我们将唯一的
方法称为public
方法(没有歧义)。如果是
(购物车为IEnumerable)。GetEnumerator()我们调用接口实现,不管它是什么(同样,我们只有一种适合的方法)

我有一个编译错误,但是为什么没有错误呢 购物车

方法应该返回
字符串
,但它不返回。添加
返回

string Ilayer2.testMethod()  // error, not all code path return a value
{
   return testMethod();
}
甚至

string Ilayer2.testMethod() => testMethod();

Ilayer2.testMethod()
中缺少一个
返回值。尝试
返回testMethod()
字符串Ilayer2.testMethod()=>testMethod()谢谢你的回答。对于
ShoppingCart作为IEnumerable).GetEnumerator()
为什么我们需要执行强制转换?@amjad:如果我们想调用
IEnumerable.GetEnumerator()
我们不能不执行强制转换-方法是
private
。我们转换为
IEnumerable
我们调用接口的方法,接口的方法都是
public
// public
public IEnumerator<Product> GetEnumerator() {...}

// private
IEnumerator IEnumerable.GetEnumerator() {...}
string Ilayer2.testMethod()  // error, not all code path return a value
{
  testMethod();
}
string Ilayer2.testMethod()  // error, not all code path return a value
{
   return testMethod();
}
string Ilayer2.testMethod() => testMethod();