ReSharper for Visual Studio Code Help

Code analysis and helpers for string literals

In system string formatting methods

ReSharper for Visual Studio Code analyzes format strings and arguments of all .NET string formatting methods, such as String.Format, Text.StringBuilder.AppendFormat, or Console.WriteLine.

If arguments and format placeholders mismatch (which leads to the FormatException in runtime if arguments are missing), ReSharper for Visual Studio Code generates warnings for missing or redundant arguments:

Warning for missing arguments in string formatting methods

You can easily fix this problem by pressing ⌥ ⏎ over the warning. ReSharper for Visual Studio Code will suggest either automatically adding the missing argument or to remove the mismatched format placeholder.

ReSharper for Visual Studio Code also helps you detect and remove redundant calls of string formatting methods inside other string formatting methods. For example:

Redundant call to string formatting method

To quickly convert a concatenation of string literals and variables, press ⌥ ⏎ anywhere within the concatenation and use the To String.Format invocation context action. For example, if we apply this context action in the return statement of the following method:

public string Greet(string name, int age) { return "Hi, my name is " + name + " and I'm " + age; }

It will be converted to:

public string Greet(string name, int age) { return String.Format("Hi, my name is {0} and I'm {1}", name, age); }

You can also use context actions to automatically add and remove format placeholders and arguments in strings. When you invoke the Insert format argument action within a string literal, ReSharper for Visual Studio Code inserts a new placeholder with the proper index and brings you to the position where you can immediately start typing the argument. This action can also be invoked on a plain string. In this case ReSharper for Visual Studio Code will automatically convert it to the String.Format call.

To quickly remove a format placeholder together with the corresponding argument, place the caret at the placeholder, press ⌥ ⏎ and choose Remove format argument.

In custom string formatting methods

To enable code analysis and assistance features in custom string formatting methods, use the [StringFormatMethod] and [StructuredMessageTemplate] attributes from the JetBrains.Annotations namespace.

Consider a custom string formatting method ShowError:

public void ShowError(string formatString, params object[] args) { // some custom logic Console.WriteLine(formatString, args); }

If the method is called incorrectly, as shown below, ReSharper for Visual Studio Code has no chance to detect the missing argument:

String formatting method called incorrectly

Make ReSharper for Visual Studio Code aware of a custom string formatting method

  1. Reference the JetBrains.Annotations namespace as described in the Annotations in source code section.

  2. Annotate your custom string formatting method with the [StringFormatMethodAttribute] attribute, which takes a single argument — the name of the format string parameter:

    [StringFormatMethod("formatString")] public void ShowError(string formatString, params object[] args) { // some custom logic Console.WriteLine(formatString, args); }

    ReSharper for Visual Studio Code will be able to warn you about missing arguments when this custom formatting method is invoked.

    Quick-fix for the string formatting method call
  3. Alternatively, annotate the parameter that accepts a formatted string with the [StructuredMessageTemplateAttribute]:

    void LogNewUser([StructuredMessageTemplate] string message, params string[] args) { // Log new user } void Test() { // Warning: Non-existing argument in format string LogNewUser("User created: {username}"); }

    This second approach allows you to use custom strings as placeholders, as username in the example above.

In interpolated strings

Modern versions of C# provide a more elegant alternative to the String.Format method — interpolated strings. That's why ReSharper for Visual Studio Code highlights usages of String.Format with the suggestion to convert them into interpolated strings. You can use a quick-fix to apply the conversion with a couple of keystrokes.

ReSharper for Visual Studio Code: Converting a usage of String.Format to string interpolation

If you need to quickly add the $ at the beginning of the string when you are deep into typing out the string, you can press ⌥ ⏎ and choose To string interpolation.

ReSharper for Visual Studio Code: Converting simple string to string interpolation
15 May 2025