Using C#'s XML comment cref attribute with params syntax
Asked Answered
O

3

18

In C#, I am trying to use <see cref="blah"/> to reference a method signature that contains the params keyword. I know this converts the parameter list to an array, but I can't even figure out how to refer to an array in a CREF attribute. I am finding nothing in my searches and no one I know has any idea, either. The compiler is choking on the square brackets. I've tried all kinds of different combinations, using curly braces, using the Array class, but nothing is working. Does anyone know this?

Oneeyed answered 17/4, 2009 at 16:20 Comment(0)
F
13

The ECMA 334 Standard PDF, Annex E contains a decent overview of XML Documentation comments. You can download the standard at:

http://www.ecma-international.org/publications/standards/Ecma-334.htm

Specifically, you'll want section E.3.1, starting on page 496.

Similar content is also at MSDN (though MSDN seems to have terrible navigation on this topic, making it difficult to find the other sections):

http://msdn.microsoft.com/en-us/library/aa664787(VS.71).aspx

The equivalent to E.3.1:

http://msdn.microsoft.com/en-us/library/aa664807(VS.71).aspx

You may also find Mono's documentation useful:

http://www.go-mono.com/docs/index.aspx?tlink=29@man%3amdoc(5)

Specfically, the "CREF FORMAT" section covers the ID string conventions.

Update 2018/05/23

The URL for the ECMA-334 standard PDF above links to the latest edition of the standard. In 2009, that was the 4th edition of the standard. However, as of December 2017, the 5th edition is current, and section E.3.1 from the 4th edition became section D.4.2 in the 5th edition.

The previous versions of the ECMA-334 standard are available for download from the following page: https://www.ecma-international.org/publications/standards/Ecma-334-arch.htm

Update 2023/11/20

Link to ECMA-334 standard is now https://ecma-international.org/publications-and-standards/standards/ecma-334/

Foreplay answered 17/4, 2009 at 19:9 Comment(2)
+1 For the research and slew of references, but I still think this answer would benefit from an example fitting the OP's requirements.Roving
This answer will be pretty useless when the links stop working.Blindfold
U
41

According to the B.3.1 ID string format article, referencing an array is done with [square brackets] (with optional lowerbound:size specifiers) but if you just want to refer to an array of a certain type (or even an Object array), you can't just write

<see cref="Object[]"/>

instead you need to specify you're making a type reference with the T: prefix, like

<see cref="T:Object[]"/>

This does not seem to apply when referencing a specific overload of a method, such as

<seealso cref="String.Join(String, String[])"/>

Unsure answered 6/5, 2009 at 1:35 Comment(1)
Looks like prefix "M:" (<seealso cref="M:String.Join(String, String[])"/>) is crucial. Especially for overloaded methods. Thanks for a nice hint.Trencher
F
13

The ECMA 334 Standard PDF, Annex E contains a decent overview of XML Documentation comments. You can download the standard at:

http://www.ecma-international.org/publications/standards/Ecma-334.htm

Specifically, you'll want section E.3.1, starting on page 496.

Similar content is also at MSDN (though MSDN seems to have terrible navigation on this topic, making it difficult to find the other sections):

http://msdn.microsoft.com/en-us/library/aa664787(VS.71).aspx

The equivalent to E.3.1:

http://msdn.microsoft.com/en-us/library/aa664807(VS.71).aspx

You may also find Mono's documentation useful:

http://www.go-mono.com/docs/index.aspx?tlink=29@man%3amdoc(5)

Specfically, the "CREF FORMAT" section covers the ID string conventions.

Update 2018/05/23

The URL for the ECMA-334 standard PDF above links to the latest edition of the standard. In 2009, that was the 4th edition of the standard. However, as of December 2017, the 5th edition is current, and section E.3.1 from the 4th edition became section D.4.2 in the 5th edition.

The previous versions of the ECMA-334 standard are available for download from the following page: https://www.ecma-international.org/publications/standards/Ecma-334-arch.htm

Update 2023/11/20

Link to ECMA-334 standard is now https://ecma-international.org/publications-and-standards/standards/ecma-334/

Foreplay answered 17/4, 2009 at 19:9 Comment(2)
+1 For the research and slew of references, but I still think this answer would benefit from an example fitting the OP's requirements.Roving
This answer will be pretty useless when the links stop working.Blindfold
S
6

You just leave out the param keyword and put in the type like this:

/// <summary>
/// <see cref="Method(string[])"/>
/// </summary>
public static void Main()
{
    Method("String1", "String2");
}

public static void Method(params string[] values)
{
    foreach (string value in values)
    {
        Console.WriteLine(value);
    }
}
Stamm answered 17/4, 2009 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.