c# - C# 允许接口属性重载的原因是什么?
问题描述
这个问题很可能会揭示我对如何创建良好界面的误解......
假设我有一个ItemClass类,它正在实现IItemInterface接口,如下所示:
public class ItemClass : IItemInterface /* ..... */
我也有以下界面:
public interface ClassInterface
{
List<IItemInterface> Items;
}
现在,假设在此接口的特定实现中,我希望我的 Items 属性比IItemInterface的简单列表更具体。C# 允许我覆盖ClassInterface Items 属性并执行此操作(代码编译/运行完美):
public class MyClass : ClassInterface
{
public List<ItemClass> Items { get; set; }
IList<IItemInterface> MyInterface.Items
{
get { return this.Items; }
}
}
// Method somewhere else
public void foo()
{
MyClass bar = new MyClass();
bar.Items = new List<ItemClass>();
// etc....
}
我的问题是:为什么 C# 允许我这样做?我知道您不允许重载属性,那么为什么对接口进行不同的处理?
解决方案
推荐阅读
- c++ - 给定这两种类型,我可以确定虚函数是否已被覆盖?
- node.js - 如何在executeJavaScript中出现单个错误后阻止BrowserWindow中断?
- asp.net-core - sendgrid 如何在没有电子邮件密码的情况下工作?
- apache-spark - rdd中的子字符串使用pyspark
- material-ui - 更改 MUI TextField 边框颜色
- regex - 正则表达式 - 匹配 3 位数字
- python - 如何检查用户输入的 str 值是否等于从 pandas 数据集填充的列表中的任何 str 值?
- java - 如何设置变量数组值
- c# - 我已将操作的安装执行顺序提供为 After="InstallInitialize",但是它在卸载时运行。如何防止它?
- c# - 自定义页面过滤器中的 ASP .NET Core 注入服务