ReSharper 2024.3 Help

Use annotations to refine code inspection

Code inspection and many other ReSharper features largely rely on knowing behavior of language constructs to detect issues, suggest possible improvements, and help you in other ways.

However, this kind of analysis cannot detect everything. For example, if a method is designed as a formatting method, no structural analysis will find a possible issue if a method invocation does not include necessary arguments.

In this and a lot of other cases, the ReSharper's JetBrains.Annotations is of a great help. By using attributes declared in this framework you can make ReSharper analyze code the way you need it. For example, you can annotate a method with the [StringFormatMethod] to indicate custom methods that work the same as System.String.Format():

[StringFormatMethod("message")] void ShowError(string message, params object[] args) { /* do something */ } void Foo(string failureDetails) { ShowError("Failed: {0}"); // Warning: Non-existing argument in format string }

This being the simplest example, there are other helpful attributes with more complex use cases. You can find the full list of these attributes in the reference.

In most cases, code annotation attributes enable specific code inspections, for example:

Also, code annotations enable more quick-fixes, code completion suggestions, and code generation features on annotated items. The annotations are also required to create and use Source templates.

ReSharper allows you to annotate code symbols in two ways:

  • You can annotate symbols in your source code as shown in the example above. In this case, you need to reference the JetBrains.Annotations namespace in your project. For more information, refer to Annotations in source code.

  • Even if you do not have access to sources, you can annotate symbols in compiled library code. For more information, refer to External annotations.

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is available in ASP.NET

Feature is available in Razor

Feature is not available in JavaScript

Feature is not available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the Languages and frameworks section.

Last modified: 11 February 2024