| 类型名 | ProvideCorrectArgumentsToFormattingMethods | 
| CheckId | CA2241 | 
| 类别 | Microsoft.Usage | 
| 是否重大更改 | 否 | 
原因
传递到方法的 format 字符串参数,例如 WriteLine、Write 或 String.Format,不包含对应于每个对象参数的格式项,反之亦然。
规则说明
方法的参数,例如 WriteLine、Write 和 Format 由一个格式字符串后接几个 System.Object 实例组成。 格式字符串由文本和 {index[,alignment][:formatString]} 形式的嵌入格式项组成。' index”是一个从零开始的整数,它指示要格式化的对象。 如果某对象在格式字符串中没有相应索引,该对象将被忽略。 如果“index”指定的对象不存在,将在运行时引发 System.FormatException。
如何解决冲突
要修复与该规则的冲突,请为每个对象参数提供一个格式项,同时为每个格式项提供一个对象参数。
何时禁止显示警告
不要禁止显示此规则发出的警告。
示例
下面的示例演示与该规则冲突的两种情况。
Imports System
Namespace UsageLibrary
   Class CallsStringFormat
      Sub CallFormat()
         Dim file As String = "file name"
         Dim errors As Integer = 13
         ' Violates the rule.
         Console.WriteLine(String.Format("{0}", file, errors))
         Console.WriteLine(String.Format("{0}: {1}", file, errors))
         ' Violates the rule and generates a FormatException at runtime.
         Console.WriteLine(String.Format("{0}: {1}, {2}", file, errors))
      End Sub
   End Class
End Namespace
using System;
namespace UsageLibrary
{
   class CallsStringFormat
   {
      void CallFormat()
      {
         string file = "file name";
         int errors = 13;
         // Violates the rule.
         Console.WriteLine(string.Format("{0}", file, errors));
         Console.WriteLine(string.Format("{0}: {1}", file, errors));
         // Violates the rule and generates a FormatException at runtime.
         Console.WriteLine(string.Format("{0}: {1}, {2}", file, errors));
      }
   }
}