ReSharper 8.0 Web Help

Code inspection and many other ReSharper features largely rely on heuristic analysis of the source code to detect issues, suggest possible improvements, and help you in other ways.

However, heuristic analysis cannot detect everything. For example, if a method is designed to never return null and its clients are designed accordingly, no heuristic analysis will not find a possible issue if someone has changed the method to return null.

In this and a lot of other cases, the Annotated Framework is of a great help. By using attributes declared in this framework you can make ReSharper analyze code the way you need it:

[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 in the Annotated Framework. You can find the full list of these attributes in the Reference.

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

  • StringFormatMethodAttribute helps detect misuses of string formatting methods.
  • CanBeNullAttribute and NotNullAttribute are associated with the Possible 'null' assignment to entity marked with 'Value cannot be null' attribute. For more information, see Value Analysis.
  • CannotApplyEqualityOperatorAttribute is bound to the Compare with '==' types marked by 'CannotApplyEqualityOperatorAttribute'.
  • ContractAnnotationAttribute introduced since ReSharper 7.0 can be used to define custom contracts for your methods and turn on the corresponding inspections. For example, you can use [ContractAnnotation("s:null => true")] to annotate a method that returns true when parameter s is null.

Also, code annotations enable more quick-fixes, code completion suggestions, and code generation features on annotated items.

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 solution. For more information, see Annotations in Source Code.
  • Even when you do not have access to sources, you can annotate symbols in compiled library code. For more information, see External Annotations.

Tip

Part of the work of annotating standard libraries (e.g. .NET Framework Class Library, NUnit Framework, and more) with external annotations is already done and the external annotations for these libraries are included in the ReSharper installation. So you can benefit from attributes driven inspections, quick-fixes, and other features when using these libraries.


This feature is supported in the following languages/technologies:

C# VB.NET HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts
feature_available feature_available feature_available feature_available

Tip

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