Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实现C#Dictionary会导致错误:没有匹配的返回类型_C#_Inheritance_Dictionary_Struct - Fatal编程技术网

实现C#Dictionary会导致错误:没有匹配的返回类型

实现C#Dictionary会导致错误:没有匹配的返回类型,c#,inheritance,dictionary,struct,C#,Inheritance,Dictionary,Struct,我试图理解C#是如何实现的。在我看来,Dictionary应该继承IEnumerable,它需要实现以下方法: IEnumerable GetEnumerator() 但是,C#字典实现了: Dictionary<T>.Enumerator GetEnumerator() 但是,这不会编译,导致以下错误: Error 2 'Foo' does not implement interface member 'IFoo.GetFoo()'. 'Foo.GetFoo()' ca

我试图理解C#是如何实现的。在我看来,Dictionary应该继承IEnumerable,它需要实现以下方法:

IEnumerable GetEnumerator()
但是,C#字典实现了:

Dictionary<T>.Enumerator GetEnumerator()
但是,这不会编译,导致以下错误:

Error   2   'Foo' does not implement interface member 'IFoo.GetFoo()'. 'Foo.GetFoo()' cannot implement 'IFoo.GetFoo()' because it does not have the matching return type of 'CodeGenerator.UnitTests.IFoo'. Foo.cs  14

你有没有想过我可能做错了什么?C#如何实现字典?如何使示例代码的编译与C#字典类似?

您缺少一个明确的接口实现:

public abstract class Foo : IFoo
{
    public abstract FooInternal GetFoo();

    // start here
    IFoo IFoo.GetFoo()
    {
        return GetFoo();
    }
    // end here

    public struct FooInternal : IFoo
    {
        public IFoo GetFoo()
        {
            return null;
        }
    }
}

缺少显式接口实现:

public abstract class Foo : IFoo
{
    public abstract FooInternal GetFoo();

    // start here
    IFoo IFoo.GetFoo()
    {
        return GetFoo();
    }
    // end here

    public struct FooInternal : IFoo
    {
        public IFoo GetFoo()
        {
            return null;
        }
    }
}

您混淆了两个不同的接口,即
IEnumerable
IEnumerator

外部类dictionary类实现了
IEnumerable
。这涉及到外部类有一个方法
GetEnumerator
。此方法返回嵌套(内部)结构的实例

内部结构实现了
IEnumerator
。要实现
IEnumerator
,必须具有
MoveNext
方法和
Current
属性

此外,还有显式接口实现的问题,Andrey Shchekin的回答中也提到了这个问题。此代码是合法的,类似于
字典

也可以通过公共方法“直接”(不显式)实现
IFoo
,但声明的返回类型必须匹配:

public class Foo : IFoo
{
  // public method that directly implements the interface
  public IBar GetBar()
  {
    return new BarInternal();
  }

  public struct BarInternal : IBar
  {
  }
}
我想,
Dictionary
之所以没有以这种更简单的方式编写,是因为您得到了嵌套结构的装箱

请注意,当您通过
字典
搜索foreach
时,C#编译器首先搜索具有确切名称
GetEnumerator
的公共非泛型无参数实例方法。如果找到这样一个方法,就会使用它,编译器不关心
IEnumerable
。因此,对于
字典
,在
foreach
期间使用的是稍微优化一点的公共方法,它不实现接口


明确的接口实现记录在MSDN上。请参阅(通用)和(非通用)。

您混淆了两个不同的接口,即
IEnumerable
IEnumerator

外部类dictionary类实现了
IEnumerable
。这涉及到外部类有一个方法
GetEnumerator
。此方法返回嵌套(内部)结构的实例

内部结构实现了
IEnumerator
。要实现
IEnumerator
,必须具有
MoveNext
方法和
Current
属性

此外,还有显式接口实现的问题,Andrey Shchekin的回答中也提到了这个问题。此代码是合法的,类似于
字典

也可以通过公共方法“直接”(不显式)实现
IFoo
,但声明的返回类型必须匹配:

public class Foo : IFoo
{
  // public method that directly implements the interface
  public IBar GetBar()
  {
    return new BarInternal();
  }

  public struct BarInternal : IBar
  {
  }
}
我想,
Dictionary
之所以没有以这种更简单的方式编写,是因为您得到了嵌套结构的装箱

请注意,当您通过
字典
搜索foreach
时,C#编译器首先搜索具有确切名称
GetEnumerator
的公共非泛型无参数实例方法。如果找到这样一个方法,就会使用它,编译器不关心
IEnumerable
。因此,对于
字典
,在
foreach
期间使用的是稍微优化一点的公共方法,它不实现接口


明确的接口实现记录在MSDN上。请参阅(通用)和(非通用)。

感谢您的详细解释!真的很有帮助。知道如何使用反射检查类型的方法是否是显式方法吗?可能有更好的方法,但根据经验,如果
methodInfo.Name.Contains(“.”
,那么它将是显式接口实现。或者这不是你要问的?好吧,显式方法是私有方法。我找到的解决方案是使用InterfaceMapping来获得显式方法。感谢您的详细解释!真的很有帮助。知道如何使用反射检查类型的方法是否是显式方法吗?可能有更好的方法,但根据经验,如果
methodInfo.Name.Contains(“.”
,那么它将是显式接口实现。或者这不是你要问的?好吧,显式方法是私有方法。我找到的解决方案是使用InterfaceMapping来获得显式方法。