We recommend using Visual Studio 2017

Provide correct arguments to formatting methods

TypeName

ProvideCorrectArgumentsToFormattingMethods

CheckId

CA2241

Category

Microsoft.Usage

Breaking Change

NonBreaking

The format argument passed to System.String.Format does not contain a format item that corresponds to each object argument, or vice versa.

The arguments to the Format method consist of a format string followed by several System.Object instances. The format string consists of text and embedded format items of the form, {index[,alignment][:formatString]}. 'index' is a zero-based integer that indicates which of the objects to format. If an object does not have a corresponding index in the format string, the object is ignored. If the object specified by 'index' does not exist, a System.FormatException is thrown at runtime.

To fix a violation of this rule, provide a format item for each object argument and provide an object argument for each format item.

Do not exclude a warning from this rule.

The following example shows two violations of the rule.

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

Show: