Open In App

Inherit Documentation in C#

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The C# programming language documentation tag <inheritdoc/> states that a documentation comment must inherit documentation from a base class or implemented interface.

Syntax

<inheritdoc [cref="link-ref"] [select="filter-expr"] />

cref=”link-ref”
This optional attribute overrides the standard search method to allow documentation inheritance from an alternate user-specified member indicated by the link-ref value.

select=”filter-expr”
This optional attribute applies a specified XPath filter expression to the inherited comments. This is useful if you want to limit the inherited documentation to a specific subset of tags or just select a particular instance or set of comments.  

For example, consider the following class hierarchy:

C#




public class Animal
{
    public virtual string MakeSound()
    {
      return "Some default sound";
    }
}
public class Dog : Animal
{
     // Use <inheritdoc/>
  // to inherit the documentation from the base class.
 public override string MakeSound()
{
return "Bark";
    }
}


In this illustration, the MakeSound method in the Dog class overrides the MakeSound method in the Animal class. Using the tag, you may see the Dog’s documentation. The documentation from the Animal will immediately be inherited by the MakeSound function. Method MakeSound. If the documentation for the base method is adequate for the derived method as well, this may be helpful.

Another Method

You can also use the <inheritdoc/> tag to inherit documentation from an interface. In this case, the tag can be used in the interface’s XML documentation comment for a member, and it will inherit documentation from the implementing class or struct.

C#




public interface IAnimal
{
    /// <summary>
    /// The name of the animal.
    /// </summary>
    string Name { get; set; }
 
    /// <summary>
    /// Makes the animal make a sound.
    /// </summary>
    void MakeSound();
}
 
public class Cat : IAnimal
{
    /// <inheritdoc/>
    public string Name { get; set; }
 
    /// <inheritdoc/>
    public void MakeSound()
    {
        Console.WriteLine("Meow!");
    }
}


Output
 Meow!

Benefits

Code Reuse: You can avoid having to provide redundant documentation for equivalent members in derived classes by inheriting documentation from a base class. This can assist guarantee that your documentation is correct and consistent while saving time and effort.

Simplicity: Because you just need to be familiar with the documentation for the base class in order to comprehend the documentation for the derived class, inheriting documentation can make it simpler to understand the documentation for a class.

Maintainability: The documentation for any derived classes that derive from a base class will automatically update whenever the base class’s documentation needs to be updated. This can make it simpler to maintain accurate and current documentation.

Consistency: Since all classes that inherit from the same base class will share identical documentation, inheritance can help ensure that your documentation is consistent across all of your classes. Users of your documentation may find it simpler to comprehend and utilize your code as a result.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads