C# 协变和继承
我很好奇为什么我的接口在抽象基类中的实现不能满足子类中的要求。这是一个示例:
public interface IBase { }
public interface IConcrete : IBase { }
public interface IBaseManager<out T>
where T : IBase
{
T Create();
IEnumerable<T> SelectAll();
}
public interface IConcreteManager : IBaseManager<IConcrete> { }
public abstract class Base : IBase { }
public class Concrete1 : Base, IConcrete { }
public abstract class BaseManager<T> : IBaseManager<T> where T : class, IBase
{
#region IBaseManager<T> Members
public T Create()
{
throw new NotImplementedException();
}
public IEnumerable<T> SelectAll()
{
throw new NotImplementedException();
}
#endregion
}
public class ConcreteManager : BaseManager<Concrete>, IConcereteManager
{
//error occurs here
}
这是正在生成的错误:
“ConsoleApplication4.ConcreteManager”未实现接口成员“ConsoleApplication4.IBaseManager
.Create()”。 “ConsoleApplication4.BaseManager
.Create()”无法实现“ConsoleApplication4.IBaseManager .Create()”,因为它没有匹配的“ConsoleApplication4.IConcrete”返回类型。< br>
如果我将这些方法添加到 ConcreteManager 类中,一切都会很好,编译器也会很高兴。
public new IConcrete Create()
{
return base.Create();
}
public new IEnumerable<IConcrete> SelectAll()
{
return base.SelectAll();
}
如果简单地返回基类方法返回的内容就足够了,为什么必须添加这些方法?为什么编译器不能调用基类中的方法?
I'm curious to know why the implementation of my interface in the abstract base class does not satisfy the the requirements in sub-classes. Here's an example:
public interface IBase { }
public interface IConcrete : IBase { }
public interface IBaseManager<out T>
where T : IBase
{
T Create();
IEnumerable<T> SelectAll();
}
public interface IConcreteManager : IBaseManager<IConcrete> { }
public abstract class Base : IBase { }
public class Concrete1 : Base, IConcrete { }
public abstract class BaseManager<T> : IBaseManager<T> where T : class, IBase
{
#region IBaseManager<T> Members
public T Create()
{
throw new NotImplementedException();
}
public IEnumerable<T> SelectAll()
{
throw new NotImplementedException();
}
#endregion
}
public class ConcreteManager : BaseManager<Concrete>, IConcereteManager
{
//error occurs here
}
This is the error that is being generated:
'ConsoleApplication4.ConcreteManager' does not implement interface member 'ConsoleApplication4.IBaseManager<ConsoleApplication4.IConcrete>.Create()'.
'ConsoleApplication4.BaseManager<ConsoleApplication4.Concrete>.Create()' cannot implement 'ConsoleApplication4.IBaseManager<ConsoleApplication4.IConcrete>.Create()' because it does not have the matching return type of 'ConsoleApplication4.IConcrete'.
If I add these methods to the ConcreteManager
class, everything is fine and the compiler is happy.
public new IConcrete Create()
{
return base.Create();
}
public new IEnumerable<IConcrete> SelectAll()
{
return base.SelectAll();
}
If simply returning what the methods from the base class return is sufficient, why do the methods have to be added? Why can't the compiler call the methods in the base class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 John 正确指出的那样,C# 语言不支持返回类型协方差。 CLR 也不支持,所以即使该语言支持它,我们实际上实现该功能的唯一方法就是默默地生成您必须自己添加的代码。
避免编写这些存根方法给开发人员带来的微小好处实际上并不能证明执行更通用的协方差功能的相当大的成本是合理的,因此我们从未这样做过。
As John points out correctly, the C# language does not support return type covariance. Neither does the CLR, so even if the language supported it, the only way we could actually implement the feature would be to silently generate exactly the code you've had to add yourself.
The small benefit afforded to developers of avoiding having to write those stub methods really does not justify the considerable cost of doing the more general covariance feature, so we've never done it.
看起来您假设返回类型协方差,因为
ConcreteManager
(作为IConcreteManager
)需要Create()
和SelectAll( )
方法,其返回类型分别为IConcrete
和IEnumerable
,这是基类不提供的。您收到这些错误是因为 C# 不支持返回类型协方差。
It looks like you're assuming return type covariance, since
ConcreteManager
(as anIConcreteManager
) expects bothCreate()
andSelectAll()
methods with a return type ofIConcrete
andIEnumerable<IConcrete>
respectively, which the base class does not provide.You are getting those errors because C# does not support return type covariance.
当您实现接口/抽象类时,必须使用相同的签名。 请参阅此处
不要让泛型让您失望,这与没有泛型没有什么不同。
When you implement an interface/abstract class, you must use the same signature. See here
Don't let the generics throw you off, this is no different than if there were no generics.