ReSharper Web Help

The easiest way to use ReSharper code annotations is to add the annotation attributes to symbols of your source code and make ReSharper analyze your solution with greater accuracy and insight.

In this topic:

Adding ReSharper code annotation support to your solution

To make use of ReSharper code annotation attributes in your solution, you should reference them in one of two ways:

  • By adding a project reference to the JetBrains.Annotations.dll, which you can find in the ReSharper installation directory.
  • By inserting attribute declarations into your solution.
To insert declarations of ReSharper code annotation attributes into your solution
  1. In the ReSharper | Options | Code Inspection | Code Annotations, click Copy default implementation to clipboard to copy declarations of ReSharper code annotation attributes.
  2. Click Cancel to close the ReSharper Options dialog box.
  3. Paste the default implementation into an empty C# code file in your project.

THe default implementation of annotation attributes is inserted in the JetBrains.Annotations namespace, but this namespace is not required for the correct functioning of the annotations. You can put these classes anywhere in your solution - ReSharper will detect them automatically. However, if you chose to do so, you should mark the containing namespace by following the procedure below.

Also, there may be cases when your solution contains several implementations of the annotation attributes, e.g., you have classes with the same names as ReSharper annotations classes (CanBeNullAttribute, NotNullAttribute, etc.) or you are using a third-party assembly that contains ReSharper annotation classes. In such cases, you can choose the namespace where ReSharper looks for the annotation attribute classes.

To manage ReSharper code annotations sources
  1. Open the ReSharper | Options | Code Inspection | Code Annotations page.
  2. All namespaces (from both source code and references assemblies) with the annotation classes are shown in the Namespaces with code annotation attributes list. Check its entries that contain the desired implementation.
  3. If there are several namespaces in the list, select the one that should be used by the ReSharper code analysis engine it in the Default annotation namespace drop-down list.
  4. Click Save to apply the modifications and let ReSharper choose the settings layer, or choose a particular settings layer in the Save To drop-down list. To learn more about saving ReSharper settings, see JetBrains .NET Tools Blog: Save or Save To in ReSharper Options.

Using context actions to add annotation attributes

As soon as ReSharper is aware of annotation classes location, you can use context actions to add the most popular annotation attributes:

  • [NotNull]
  • [CanBeNull]
  • [UsedImplicitly]

To add these annotations, press Alt+EnterAlt+Enter on the symbol you want to annotate, and ReSharper will suggest an attribute depending on the context:

annotation_ca