Search code examples
c#code-documentationndoc

What Are Best Practices For Documenting C# code with XML comments?


I'm going through some new code I just wrote and adding NDoc sytle comments to my classes and methods. I'm hoping to generate a pretty good MSDN style document for reference.

In general, what are some good guidelines when writing comments for a class and for a method? What should the NDoc comments say? What should they not say?

I find myself looking at what the .NET framework comments say, but that gets old fast; if I could have some good rules to guide myself, I could finish my docs a lot faster.


Solution

  • In comments used to build API documentation, you should:

    • Explain what the method or property does, why it exists at all, and explain any domain concepts that are not self-evident to the average consumer of your code.

    • List all preconditions for your parameters (cannot be null, must be within a certain range, etc.)

    • List any postconditions that could influence how callers deal with return values.

    • List any exceptions the method may throw (and under what circumstances).

    • If similar methods exist, explain the differences between them.

    • Call attention to anything unexpected (such as modifying global state).

    • Enumerate any side-effects, if there are any.