Annotations in Source Code
The easiest way to benefit from ReSharper's 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.
By default, all annotation attribute classes are marked with
so the compiler will ignore the attribute usages in your code,
which means that no binary reference to the 'JetBrains.Annotations.dll' assembly will be produced.
However, you can define 'JETBRAINS_ANNOTATIONS' conditional compilation symbol in your projects
to preserve the attributes in metadata.
In this topic:
- Enabling ReSharper code annotations support to your project
- Using Context Actions to add annotation attributes
Enabling ReSharper code annotations support to your project
To make use of ReSharper code annotation attributes in your project, you should reference them in one of the following ways:
The recommended way is to install the
Actually, you do not even need to go to the NuGet website to get the package. Just add the
using JetBrains.Annotations;directive, and use the corresponding context action to automatically fetch the package.
- You can simply add a project reference to the JetBrains.Annotations.dll, which you can find in the ReSharper installation directory. .
You can also insert attribute declarations anywhere in your project.
If necessary, you can insert them into other namespace.
You can copy the declarations in the Code Inspection | Code Annotations options page. Optionally, you can choose to make the declarations internal and/or add the
[Conditional("JETBRAINS_ANNOTATIONS")]attribute to the declarations.
If the JetBrains.Annotations.dll is referenced or attribute declarations are inserted in any of the projects within your solution, you can type the desired annotation attribute and press Alt+Enter to invoke the quick-fix that will reference this module in the current project and add the missing namespace import directive:
Alternatively, you can press Ctrl+Alt+Space twice to invoke the double import completion, which will find the attribute, import the namespace, and add the reference.
The default implementation of the annotation attributes is inserted in the
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 select the proper namespace by following the procedure below.
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
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 should look for the proper set of
annotation attribute classes.
To manage ReSharper code annotations sources
- Open the ReSharper | Options | Code Inspection | Code Annotations page.
All namespaces (from both source code and references assemblies) other than
JetBrains.Annotationswith the annotation classes are shown in the Namespaces with code annotation attributes list. Check the entries that contain the desired implementation.
- 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.
- Click Save to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To drop-down list. For more information, see Managing and Sharing ReSharper Settings.
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:
To add these annotations, press Alt+Enter on the symbol you want to annotate, and ReSharper will suggest an attribute depending on the context: