Skip to content

Gendarme.Rules.BadPractice.PreferParamsArrayForVariableArgumentsRule(2.10)

Sebastien Pouliot edited this page Feb 9, 2011 · 3 revisions

PreferParamsArrayForVariableArgumentsRule

Assembly: Gendarme.Rules.BadPractice
Version: 2.10

Description

The rule warns for any method that use the (semi-documented) vararg calling convention (e.g. __arglist in C#) and that is not used for interoperability (i.e. pinvoke to unmanaged code). Using params (C#) can to achieve the same objective while vararg is not CLS compliant. The later will limit the usability of the method to CLS compliant language (e.g. Visual Basic does not support vararg.

Examples

Bad example:

public void ShowItems_Bad (string header, __arglist)
{
    Console.WriteLine (header);
    ArgIterator args = new ArgIterator (__arglist);
    for (int i = 0; i < args.GetRemainingCount (); i++) {
        Console.WriteLine (__refvalue (args.GetNextArg (), string));
    }
}

Good example:

public void ShowItems (string header, params string [] items)
{
    Console.WriteLine (header);
    for (int i = 0; i < items.Length; i++) {
        Console.WriteLine (items [i]);
    }
}

Good example (interoperability):

[DllImport ("libc.dll")]
static extern int printf (string format, __arglist);

Notes

  • This rule is available since Gendarme 2.8

Clone this wiki locally