Skip to content

Gendarme.Rules.BadPractice.PreferParamsArrayForVariableArgumentsRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

PreferParamsArrayForVariableArgumentsRule

Assembly: Gendarme.Rules.BadPractice
Version: git

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

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally