Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comment Inheritance for C# (actually any language)

Suppose I have this interface

public interface IFoo {     ///<summary>     /// Foo method     ///</summary>     void Foo();      ///<summary>     /// Bar method     ///</summary>     void Bar();      ///<summary>     /// Situation normal     ///</summary>     void Snafu(); } 

And this class

public class Foo : IFoo {     public void Foo() { ... }     public void Bar() { ... }     public void Snafu() { ... } } 

Is there a way, or is there a tool that can let me automatically put in the comments of each member in a base class or interface?

Because I hate re-writing the same comments for each derived sub-class!

like image 715
jumpinjackie Avatar asked Dec 05 '08 05:12

jumpinjackie


People also ask

What is inheritance in C?

Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them. Inheritance is almost like embedding an object into a class.

Is inheritance possible in C?

No it doesnt. C is not an Object Oriented language. Inheritance is a property of OO languages.

Which is the correct syntax of inheritance in C?

Which is the correct syntax of inheritance? Explanation: Firstly, keyword class should come, followed by the derived class name. Colon is must followed by access in which base class has to be derived, followed by the base class name.

What is the need of inheritance in C++?

Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.


2 Answers

You can always use the <inheritdoc /> tag:

public class Foo : IFoo {     /// <inheritdoc />     public void Foo() { ... }     /// <inheritdoc />     public void Bar() { ... }     /// <inheritdoc />     public void Snafu() { ... } } 

Using the cref attribute, you can even refer to an entirely different member in an entirely different class or namespace!

public class Foo {     /// <inheritdoc cref="System.String.IndexOf" />     public void Bar() { ... } // this method will now have the documentation of System.String.IndexOf } 
like image 148
Vadim Avatar answered Nov 15 '22 16:11

Vadim


Use /// <inheritdoc/> if you want inheritance. Avoid GhostDoc or anything like that.

I agree it is annoying that comments are not inherited. It would be a fairly simple add-in to create if someone had the time (i wish i did).

That said, in our code base we put XML comments on the interfaces only and add extra implementation comments to the class. This works for us as our classes are private/internal and only the interface is public. Any time we use the objects via the interfaces we have full comments display in intellisence.

GhostDoc is good start and has made the process easier to write comments. It is especially useful keeping comments up-to-date when you add/remove parameters, re-run GhostDoc and it will update the description.

like image 43
Dennis Avatar answered Nov 15 '22 16:11

Dennis