XML documentation of Action parameters in C#

1.8k views Asked by At

I am looking for a way to document action parameters in C#. Code looks like this:

/// <summary>
/// Do some action with parameters.
/// </summary>
/// <params name="someAction"> This is action but what about parameters? </param>
public void Do(Action<int,int,string> someAction)

How can I name and document parameters of "someAction" so that they would appear in intellisense?

2

There are 2 answers

0
Guney Ozsan On BEST ANSWER

You can use a nested structure.

/// <summary>
/// Does something useful.
/// </summary>
/// <param name="c">A constant</param>
/// <param name="someAction">
///     Here is an action.
///     <param name="someAction arg1">Count</param>
///     <param name="someAction arg2">Length</param>
///     <param name="someAction arg2">Content</param>
/// </param>
public void Do(int c, Action<int,int,string> someAction){}

It looks like this in the editor:

enter image description here

However, for complex structures, this is harder to maintain in the long run. If the context is not apparent intuitively, it is better and safer to create a class.

public class MyActionArgs : ActionArgs
{
    public MyActionArgs(int count, int length, string content)
    {
        Count = count;
        Length = length;
        Content = content ?? throw new ArgumentNullException(nameof(content));
    }

    public int Count { get; }
    public int Length { get; }
    public string Content { get; }
}

public void Do(Action<MyActionArgs> myAction){} 
0
Optional Option On

In short - not possible. Few possible workarounds:

  • Use a class or a structure instead of multiple parameters. Eg. instead of Action<int,int,int> use Action<ThreePigletsClass>
  • Minimize the number of parameters if possible. Maybe some are not necessary and simple Action would work.
  • Write parameter comments... which does not help much but gets you Stackoverflow points.
  • If not alergic to delegates use them instead.