[英]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.