简体   繁体   English

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

[英]Why can't I use HashSet<string> to implement an IEnumerable<string> interface property?

I was wondering why can't I use HashSet<string> to implement an IEnumerable<string> interface property ? 我想知道为什么不能使用HashSet<string>来实现IEnumerable<string>接口属性?

The code below gives me a compile error with the following error; 下面的代码给我一个编译错误,并显示以下错误;

'Lookups' does not implement interface member 'ILookups.LastNames'. “查找”未实现接口成员“ ILookups.LastNames”。 'Lookups.LastNames' cannot implement 'ILookups.LastNames' because it does not have the matching return type of 'System.Collections.Generic.IEnumerable'. '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; }
}

According to Resharper, this is the constructor signature for HashSet ; 根据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
  {

...and it looks like it definitely implements IEnumerable<T> huh! ...而且看起来它肯定实现了IEnumerable<T>呵呵! This is not critical, just annoying as the workaround is just lengthy and feels like a broken feature of the language and very er..Java-ish? 这不是很关键,只是很烦人,因为解决方法很长,感觉像是该语言的一个损坏功能,而且非常错误。 (he he!). (呵呵!)。 (I will post the work around here later as soon as it's done in case I've missed a trick). (一旦完成,我将在稍后将工作发布在这里,以防万一我错过了一个窍门)。 If anyone has an answer or a better way to do this, or why this is like this, that would be most appreciated? 如果有人对此有答案或更好的方法,或者为什么会这样,那将不胜感激?

txs, txs,

Alan 艾伦

UPDATE:1.1.15 1 day after most of the comments were written, so please take with a pinch of salt. 更新:1.1.15大多数评论写完后的第一天,所以请带一点盐。

re: re: "You can't implement a property that's declared to return A with another one that returns B even when B inherits/implements A." re:re:“即使B继承/实现A,也不能实现声明为返回A的属性和另一个返回B的属性。” I don't believe this is entirely correct, as the following code compiles perfectly ok; 我不认为这是完全正确的,因为以下代码完全可以编译; doh! h!

 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; }
}

I've just seen my mistake, the code above will not compile if you change Person GetPerson(int id) to Manager GetPerson(int id) you will get a compile error, which actually does make sense! 我刚刚看到了我的错误,如果将Person GetPerson(int id)更改为Manager GetPerson(int id) ,则上面的代码将无法编译,您将得到一个编译错误,这实际上是有道理的! Ok, I think this is done and dusted! 好吧,我认为这已经完成并撒了粉! ;-D ;-D

To implement interface member signature must be exactly the same as declared in interface. 要实现接口,成员签名必须与interface中声明的完全相同。 You can't implement a property that's declared to return A with another one that returns B even when B inherits/implements A . 即使B继承/实现A ,也不能实现声明为返回A的属性和另一个返回B的属性。

You can implement that member expliticly and route it to your property: 您可以显式实现该成员并将其路由到您的媒体资源:

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

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

Why is that required? 为什么要这样? Consider following code: 考虑以下代码:

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

How would you like this to be solved? 您希望如何解决? It's totally valid code, but with your Lookups implementation you've just assigned List<string> to HashSet<string> ! 这是完全有效的代码,但是通过您的Lookups实现,您刚刚将List<string>分配给了HashSet<string> It doesn't matter for methods and/or getter-only properties, but maybe just for sake of consistency? 对于方法和/或仅具有吸气剂的属性来说并不重要,但仅仅是出于一致性考虑?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能使用以下IEnumerable <string> ? - Why can't I use the following IEnumerable<string>? 为什么我不能通过字典 <string, string> 到IEnumerable <KeyValuePair<string, string> &gt;作为通用类型 - Why can't i pass Dictionary<string, string> to IEnumerable<KeyValuePair<string, string>> as generic type 我可以用IEnumerable编写此lambda吗 <T> 而不是IEnumerable <string> ? - Can I write this lambda with IEnumerable<T> rather than IEnumerable<string>? 如何实现具有IEnumerable类型的属性的接口 <IA> 并使用实现该类型的接口的类型覆盖? - How can I implement an interface with a property of type IEnumerable<IA> and override with a type that implements the interface of the type? 如果我只能定义一个GetEnumerator,为什么还要实现IEnumerable(T)? - Why implement IEnumerable(T) if I can just define ONE GetEnumerator? IEnumerable的 <IEnumerable<T> &gt;转换为字符串[] [] - IEnumerable<IEnumerable<T>> to string[][] 我该如何投射IEnumerable <?> 到IEnumerable <string> ? - How can I cast IEnumerable<?> to IEnumerable<string>? 我可以合并两个IEnumerable <string> 到IEnumerable <string,string> - Can I merge two IEnumerable<string> to IEnumerable<string,string> 如何实现 IEnumerable<T> 界面? - How to implement IEnumerable<T> interface? 为什么我不能预先分配一个哈希集<t></t> - Why can't I preallocate a hashset<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM