Inherit XML Documentation Comments in C#

I am a big fan of the XML documentation comments in C#. This documentation is directly in the code and has therefore the biggest chance to be updated when the code changes.

However, when you demand to document all public methods, you end up with a lot of repetitive documentation. As with duplicated code, you will update only the parts you remember, and the others will be outdated in no time. That quickly negates the benefits of having the documentation in the first place.

A simple solution for this problem is to inherit the documentation that should be the same. We can do that with the keyword inheritdoc.

This keyword allows us to document the interface with all the necessary details only once and then inherit this documentation in our implementations:

In the class that implements our interface we only need to document what makes this implementation special while we reuse the documentation for the methods:

Not only do you need to write less documentation, if things change, we only need to update it at one place. In Visual Studio everything looks as if we wrote the documentation on the class itself:

The code documentation works as expected

Wherever we used this approach, the developers stopped to bother writing slightly varying comments for the different implementations. Instead, that time was used to think a bit more about the documentation of the interface, what leads to more useful descriptions. Try it!

3 thoughts on “Inherit XML Documentation Comments in C#”

  1. I have tried doing this and the comment does not show up when I hover, is there a setting required to turn it on?

    Reply
    • Hi Issun,
      It may be a ReSharper feature that is not present in the basic Visual Studio edition.

      Regards,
      Johnny

      Reply
  2. When I use inheritdoc, I don’t know why, but it doesn’t show me the “param” documentation.
    Any idea why?

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.