A composite format string is a string that contains
placeholders, represented by indices inside curly braces "{0}", "{1}", etc. These placeholders are replaced by values when the string is printed or
logged.
Because composite format strings are interpreted at runtime, rather than validated by the compiler, they can contain errors that lead to unexpected
behaviors or runtime errors.
This rule validates the correspondence between arguments and composite formats when calling the following methods:
Exceptions
- No issue is raised if the format string is not a string literal, but comes from a variable.
var pattern = "{0} {1} {2}";
var res = string.Format(pattern, 1, 2); // Incorrect, but the analyzer doesn't raise any warnings here
- No issue is raised if the argument is not an inline created array.
var array = new int[] {};
var res = string.Format("{0} {1}", array); // Compliant; we don't know the size of the array
- This rule doesn’t check whether the format specifier (defined after the
:
) is actually valid.