【问题标题】:How to extend interface c#?如何扩展接口c#?
【发布时间】:2022-01-14 22:17:57
【问题描述】:

我在 c# 中有一个接口,如下所示:

public interface IMyInterfaceA 
{
        string Name { get; }
        int Id { get; set; }
}

我想扩展此接口以包含其他属性。

例如:

public interface IMyInterfaceB: IMyInterfaceA
{
        string newProp { get; }
       
}

问题是这样的语法是无效的。它要求我实现 IMyInterfaceA, 所以我的界面看起来像:

public interface IMyInterfaceB: IMyInterfaceA
{
        string newProp { get; }
         public string Vendor => throw new NotImplementedException();

        public int VendorId { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}

我使用继承的所有目标是让我不需要再次拥有这些属性。

IMyInterfaceA是我无法更改的核心接口。

如何在不重写基本属性的情况下实现扩展?

编辑: 我的代码没有错误。我错误地创建了一个类而不是一个接口。

【问题讨论】:

  • “问题是这样的语法无效。”嗯,很确定是的;否则到底是怎么说的,它说了什么?
  • 你收到了什么错误信息?
  • 如果我不实现IMyInterfaceA(例如添加属性),它会在IMyInterfaceB: IMyInterfaceA这个词后面显示红线并说“实现接口”
  • 问题中的代码没问题。你确定你没有创建 class IMyInterfaceB : IMyInterfaceA 吗? (注class)
  • 天哪!你说得对!我会删除帖子。 Tnx。

标签: c# interface


【解决方案1】:

您的代码示例没有问题。接口支持从另一个接口继承而不实现它的成员。也许您的示例与出现问题的代码不同。

【讨论】:

    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2020-09-28
    • 1970-01-01
    相关资源
    最近更新 更多