다음을 통해 공유


CA2230: Use params for variable arguments

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName UseParamsForVariableArguments
CheckId CA2230
Category Microsoft.Usage
Breaking Change Breaking

Cause

A public or protected type contains a public or protected method that uses the VarArgs calling convention.

Rule Description

The VarArgs calling convention is used with certain method definitions that take a variable number of parameters. A method using the VarArgs calling convention is not Common Language Specification (CLS) compliant and might not be accessible across programming languages.

In C#, the VarArgs calling convention is used when a method's parameter list ends with the __arglist keyword. Visual Basic does not support the VarArgs calling convention, and Visual C++ allows its use only in unmanaged code that uses the ellipse ... notation.

How to Fix Violations

To fix a violation of this rule in C#, use the params keyword instead of __arglist.

When to Suppress Warnings

Do not suppress a warning from this rule.

Example

The following example shows two methods, one that violates the rule and one that satisfies the rule.

using System;

[assembly: CLSCompliant(true)]
namespace UsageLibrary
{
    public class UseParams 
    {
        // This method violates the rule.
        [CLSCompliant(false)]
        public void VariableArguments(__arglist) 
        { 
            ArgIterator argumentIterator = new ArgIterator(__arglist);
            for(int i = 0; i < argumentIterator.GetRemainingCount(); i++) 
            { 
                Console.WriteLine(
                    __refvalue(argumentIterator.GetNextArg(), string));
            } 
        }

        // This method satisfies the rule.
        public void VariableArguments(params string[] wordList)
        { 
            for(int i = 0; i < wordList.Length; i++) 
            { 
                Console.WriteLine(wordList[i]);
            } 
        }
    }
}

See Also

System.Reflection.CallingConventions Language Independence and Language-Independent Components