MsCProg
MsCProg

Reputation: 11

C# - How to inherit documentation from hidden methods (new keyword)

Is there a way not to hide the documentation of the original method in the child class? For example;

    public class Parent
    {
        /// <summary>
        /// Boo!!!
        /// </summary>
        public void Foo()
        {
            // ... code here
        }
    }

    public class Child : Parent
    {
        // inheritdoc doesn't work here

        /// <inheritdoc/>
        public new void Foo()
        {
            base.Foo();
            // ... additional logic here
        }
    }

Upvotes: 0

Views: 153

Answers (1)

themassiveone
themassiveone

Reputation: 71

As Muhammad just said: By declaring a method with the new keyword, you basically create a new method signature, that is not related to the old method in any way. A virtual method however overrides the old method instead of covering it up. Therefore overriding a method will inherit its documentation.

public class Parent
{

    /// <summary>
    ///  a classic method documentation
    /// </summary>
    public void Test1()
    {

    }

    /// <summary>
    /// a virtual method documentation
    /// </summary>
    public virtual void Test2()
    {

    }
}


public class Child : Parent
{
    /// <inheritdoc/>
    // does not inherit documentation
    public new void Test1()
    {
        base.Test1();
    }

    /// <inheritdoc/>
    // does inherit documentation
    public override void Test2()
    {
        base.Test2();
    }
}

Upvotes: 2

Related Questions