0

In Java, it is a common practice to redefine JavaDoc comments in interfaces extending other interfaces:

interface Foo {
/**
  * Basic getGadget information
  */
  String getGadget();
}

interface Bar extends Foo {
/**
  * More specific getGadget description, in context of Bar
  */
  @Override
  String getGadget();
}

Is there a way to achieve similar goal in C#?

With a direct translation:

interface IFoo
{
    /// <summary>
    /// Basic GetGadget information
    /// </summary>
    string GetGadget();
}

interface IBar : IFoo
{
    /// <summary>
    /// More specific GetGadget description, in context of IBar
    /// </summary>
    string GetGadget();
}

the compiler warns on member hiding:

  IFoo.cs(16, 16): [CS0108] 'IBar.GetGadget()' hides inherited member 'IFoo.GetGadget()'. Use the new keyword if hiding was intended.

Edit

  • My goal is ONLY to have a more specific comment.
  • I still want to call GetGadget via IFoo instance and get the method from IBar
  • new allows following implementation if IBar, which I want to avoid (conceptually, there is only one method)
class C : IBar
{
    string IFoo.GetGadget()
    {
        throw new System.NotImplementedException();
    }

    string IBar.GetGadget()
    {
        throw new System.NotImplementedException();
    }
}
Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • @TravelingTechGuy you can't override interface methods in C#: https://stackoverflow.com/questions/38273352/why-cant-i-override-my-interface-its-methods – Code Stranger Mar 10 '20 at 17:13
  • @TravelingTechGuy No `The modifier 'override' is not valid for interface member declaration. Only 'new' is valid.` – Lesiak Mar 10 '20 at 17:13
  • Someone suggested using `new`. Is this a valid and idiomatic way to accomplish my goal? – Lesiak Mar 10 '20 at 17:15
  • 2
    @Lesiak it's valid if that's what you're looking for.. You can't overload here because there isn't an implementation that's available to be overridden. It's just a contract. If you want `IBar.GetGadget` to hide `IFoo.GetGadget`, then go for it. If that isn't what you wan't, you'll need to be a little more clear on what you're after. If it's *just* to utilize separate comments, then no, it's not valid IMO. – Broots Waymb Mar 10 '20 at 17:17
  • 2
    @Lesiak it depends. The `new` keyword implies that the contract implied by `GetGadget()` on `IBar` is somehow different than the contract implied by `GetGadget()` on `IFoo`. If they're intended for the same purpose, then it's not idiomatic to write a new comment, and perhaps the comment on `GetGadget()` of `IFoo` was too specific. – Patrick Roberts Mar 10 '20 at 17:18
  • @PatrickRoberts They are intended to serve the same purpose, more specific comment reflects additional knowledge on the returned value. The goal is ONLY to have more specific comment. I still want to call `GetGadget` via `IFoo` instance and get the method from `IBar` – Lesiak Mar 10 '20 at 17:22
  • 5
    The short and harsh answer is that you cannot override the comment on the interface. You _can_ set a different comment on a class method that implements the interface method, though I don't think you will achieve what you get in Java – Peter Lillevold Mar 10 '20 at 17:42
  • 1
    Thanks @PeterLillevold, fully understood. No 2 languages are feature-equivalent. – Lesiak Mar 10 '20 at 17:49

0 Answers0