共用方式為


CA1025: Replace repetitive arguments with params array

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. 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
RuleId CA1025
Category Microsoft.Design
Breaking change Non-breaking

Cause

A public or protected method in a public type has more than three parameters, and its last three parameters are the same type.

Note

This rule has been deprecated. For more information, see Deprecated rules.

Rule description

Use a parameter array instead of repeated arguments when the exact number of arguments is unknown and the variable arguments are the same type, or can be passed as the same type. For example, the WriteLine method provides a general-purpose overload that uses a parameter array to accept any number of Object arguments.

How to fix violations

To fix a violation of this rule, replace the repeated arguments with a parameter array.

When to suppress warnings

It is always safe to suppress a warning from this rule; however, this design might cause usability issues.

Example

The following example shows a type that violates this rule.

using System;

namespace DesignLibrary
{
   public class BadRepeatArguments
   {
      // Violates rule: ReplaceRepetitiveArgumentsWithParamsArray.
      public void VariableArguments(object obj1, object obj2, object obj3, object obj4) {}
      public void VariableArguments(object obj1, object obj2, object obj3, object obj4, object obj5) {}
   }

   public class GoodRepeatArguments
   {
       public void VariableArguments(object obj1) {}
       public void VariableArguments(object obj1, object obj2) {}
       public void VariableArguments(object obj1, object obj2, object obj3) {}
       public void VariableArguments(params Object[] arg) {}
   }
}