ReSharper Web Help

You can use attributes in the JetBrains.Annotations namespace to annotate your own code and make ReSharper analyze it with greater accuracy and insight - for example, AssertionMethodAttribute and AssertionConditionAttribute are perfect to annotate custom assertion methods, as follows:

[AssertionMethod]
public static void Assert(
            [AssertionCondition(AssertionConditionType.IS_TRUE)] bool condition,
            [NotNull] string message)
{}        

Another example of how custom attributes can be used is marking a method that never returns a null value with NotNull attribute, to prevent ReSharper from showing the Possible null-reference exception warning:

[NotNull]
public static TValue TryGetValue<TKey, TValue>(
            [NotNull] this IDictionary<TKey, TValue> dictionary,
            TKey key)
{}
    

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

  • By adding a project reference to the JetBrains.Annotations.dll in the ReSharper installation directory (or elsewhere if you chose to copy this assembly).
  • By copying and pasting attribute declarations into your solution.

Note

For guidelines on how to add a project reference, see MSDN: Adding and Removing Project References.

To copy and paste 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.
  4. If required, move the default implementation to another namespace.

If you choose to transfer the default implementation into an alternative namespace, you should mark that namespace as a container for declarations of the attributes by following the procedure below.

To mark a namespace as containing declarations of ReSharper code annotation attributes and set a default annotation namespace
  1. Open the ReSharper | Options | Code Inspection | Code Annotations page.
  2. The namespace where you've transferred the code annotation attributes should be now listed in the Namespaces with code annotation attributes area. Check its entry to mark the namespace as containing the declarations of the attributes.
  3. Optionally, to configure this namespace as the default code annotation namespace, select it in the Default annotation namespace drop-down list in the lower part of the tab.
  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.