为什么我不能使用HashSet <string>来实现IEnumerable <string>接口属性?

sno*_*ode 1 c# interface hashset

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

下面的代码给出了一个编译错误,出现以下错误;

'Lookups'没有实现接口成员'ILookups.LastNames'.'Lookups.LastNames'无法实现'ILookups.LastNames',因为它没有匹配的返回类型'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; }
}
Run Code Online (Sandbox Code Playgroud)

根据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
  {
Run Code Online (Sandbox Code Playgroud)

......看起来它肯定会实现IEnumerable<T>吧!这并不重要,只是烦人,因为解决方法只是冗长而感觉就像语言的破坏特征而且非常......哇哇哇哇哇哇哇哇哇 (呵呵!).(如果我错过了一个技巧,我会尽快在这里发布工作).如果有人有答案或更好的方法来做到这一点,或者为什么会这样,那将是最受欢迎的?

TXS,

艾伦

更新:1.1.15大部分评论写完后1天,所以请带上一小撮盐.

re:re:"即使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; }
}
Run Code Online (Sandbox Code Playgroud)

我刚刚看到我的错误,如果你改变上面的代码不会编译Person GetPerson(int id)Manager GetPerson(int id)你会得到一个编译错误,这实际上有一定道理!好吧,我认为这已经完成并且已经尘埃落定了!;-D

Mar*_*zek 8

实现接口成员签名必须与接口中声明的完全相同.您无法实现声明为A与另一个返回的属性,B即使在B继承/ implements 时也会返回该属性A.

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

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

    IEnumerable<string> ILookups.FirstNames { get { return this.FirstNames; } }
}
Run Code Online (Sandbox Code Playgroud)

为什么需要这个?考虑以下代码:

var lookups = (ILookups)new Lookups();
// assigning List<string> to ILookups.FirstNames, which is IEnumerable<string>
lookups.FirstNames = new List<string>();
Run Code Online (Sandbox Code Playgroud)

你想怎么解决这个问题?这是完全有效的代码,但您Lookups刚刚分配List<string>到您的实现HashSet<string>!方法和/或仅具有吸气剂的属性无关紧要,但可能只是为了保持一致性?