ReSharper 2017.2 Help

Using 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 to never return null and its clients are designed accordingly, no structural analysis will find a possible issue if someone has changed the method to return null.

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:

[NotNull] public object Foo() { return null; // Warning: Possible 'null' assignment }

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 JetBrains.Annotations namespace in your project. For more information, see Annotations in Source Code.
  • Even if you do not have access to sources, you can annotate symbols in compiled library code. For more information, see 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 Feature is available Feature is not available Feature is not available Feature is available Feature is available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

Last modified: 14 December 2017

See Also