c# - 为什么我不能使用 HashSet<string> 来实现 IEnumerable<string> 接口(interface)属性?

标签 c# interface hashset

我想知道为什么我不能使用 HashSet<string>实现 IEnumerable<string>接口(interface)属性?

下面的代码给了我一个带有以下错误的编译错误;

'Lookups' does not implement interface member 'ILookups.LastNames'. 'Lookups.LastNames' cannot implement 'ILookups.LastNames' because it does not have the matching return type of 'System.Collections.Generic.IEnumerable'.


public interface ILookups
{
    IEnumerable<string> FirstNames { get; set; }
    IEnumerable<string> LastNames { get; set; }
    IEnumerable<string> Companies { get; set; }
}

public class Lookups : ILookups
{
    public HashSet<string> FirstNames { get; set; }
    public HashSet<string> LastNames { get; set; }
    public HashSet<string> Companies { get; set; }
}

根据 Resharper,这是 HashSet 的构造函数签名。 ; ...
// Type: System.Collections.Generic.HashSet`1
// Assembly: System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Core.dll
...
  /// <summary>
  /// Represents a set of values.
  /// </summary>
  /// <typeparam name="T">The type of elements in the hash set.</typeparam>
  [DebuggerDisplay("Count = {Count}")]
  [DebuggerTypeProxy(typeof (HashSetDebugView<>))]
  [__DynamicallyInvokable]
  [Serializable]
  [HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
  public class HashSet<T> : ISerializable, 
      IDeserializationCallback, ISet<T>, 
      ICollection<T>, IEnumerable<T>, IEnumerable
  {

...看起来它确实实现了 IEnumerable<T>啊!这并不重要,只是烦人,因为解决方法很长,感觉像是语言的一个损坏的特性,而且非常呃..Java-ish? (呵呵!)。 (一旦完成,我将在此处发布工作,以防我错过了一个技巧)。如果有人有答案或更好的方法来做到这一点,或者为什么会这样,那将不胜感激?

发送,

艾伦

更新:1.1.15 大部分评论写完后 1 天,所以请多加一点盐。

回复:回复:“即使 B 继承/实现 A,您也不能实现一个声明为返回 A 的属性与另一个返回 B 的属性。”我不认为这是完全正确的,因为以下代码编译得很好;哦!
 void Main()
{
    var r = new PersonRepo();
    Console.WriteLine(r.GetPerson(2).Name);
}

public class PersonRepo : IPersonRepo
{
    public Person GetPerson(int id)
    {
        var m = new Manager()
        { 
            Department = "department" + id.ToString(), 
            Name = "Name " + id.ToString()
        };
        return m;
    }
}

public interface IPersonRepo
{
    Person GetPerson(int id);
}

public class Person 
{
    public string Name { get; set;}
}

public class Manager : Person
{
    public string Department { get; set; }
}

我刚刚看到我的错误,如果您更改 Person GetPerson(int id),上面的代码将无法编译至Manager GetPerson(int id)你会得到一个编译错误,这实际上是有道理的!好的,我认为这已经完成并除尘了! ;-D

最佳答案

实现接口(interface)成员签名必须与接口(interface)中声明的完全相同。您不能实现声明为返回 A 的属性与另一个返回 B即使 B继承/实现A .

您可以显式地实现该成员并将其路由到您的属性:

public class Lookups : ILookups
{
    public HashSet<string> FirstNames { get; set; }

    IEnumerable<string> ILookups.FirstNames { get { return this.FirstNames; } }
}

为什么需要这样做?考虑以下代码:
var lookups = (ILookups)new Lookups();
// assigning List<string> to ILookups.FirstNames, which is IEnumerable<string>
lookups.FirstNames = new List<string>();

您希望如何解决这个问题?这是完全有效的代码,但使用您的 Lookups您刚刚分配的实现 List<string>HashSet<string> !方法和/或 getter-only 属性无关紧要,但也许只是为了保持一致性?

关于c# - 为什么我不能使用 HashSet<string> 来实现 IEnumerable<string> 接口(interface)属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27713137/

相关文章:

c# - LabelFor不显示值?

c# - 如何在 C++/Cli 中使用可等待对象

C# 缩放 GDI 位置但不缩放字体大小或线条粗细

java - DAO 接口(interface)

java - 通过输入 HashMap 值获取 HashMap key 无法按预期工作

c# - 将 JSON 转换为 C# 复杂对象

json - 如何在 Go 中导航具有未知结构的嵌套 json?

c# - 创建接口(interface)时的差异

C#类型转换: Explicit cast exists but throws a conversion error?

c++ - 分配给数组成员时 Clang 中的 "Read-only variable is not assignable"