Alex Alex - 9 months ago 55
C# Question

In C# XML comments, how can I refer a parameter of another method?

In MethodB XML doc, I want to write something like

/// <remarks>Before calling this method, call <see cref="MethodA"> passing zero as <paramef name="MethodA.MyParam"/> value.

MethodA is declared like

void MethodA(int MyParam)

Is it possible? Currently, I have to write it this way:

/// <remarks>Before calling this method, call <see cref="MethodA"> passing zero as <i>MyParam<i/> value.

Of course, this leads to potential errors as the compiler no longer warns me that the parameter name is wrong if I made a typo or changed the name in the code.

Answer Source

You can't.

What you can, however is to declare an IsReady-like public property and set it to true when MethodA is called properly.

MethodB should check that property and throw an InvalidOperationException if its value is false. Then add this XML coment to MethodB:

/// <exception cref="InvalidOperationException">
///     <see cref="IsReady" /> is <c>false<c>.
/// </exception>

This way the state of your instance is publicly available, so a caller does not need to know whether MethodA is called with the right arguments beforehand, it can just check whether the IsReadyis true.

Passing what argument would make the instance "ready" should be in the MethodA's own documentation, not in the MethodB's, in my opinion.