This guide will walk you through installing CommentSense, configuring your project, and experiencing the core features including diagnostics and automatic code fixes.
This quick start assumes you have a C# project using .NET 8.0 or later. CommentSense works with any .NET project that supports Roslyn analyzers.
Let’s create a simple C# class to see CommentSense in action. Create a new file called Calculator.cs:
Calculator.cs
public class Calculator{ public int Add(int a, int b) { return a + b; } public int Divide(int numerator, int denominator) { if (denominator == 0) throw new ArgumentException("Cannot divide by zero"); return numerator / denominator; }}
/// <summary>/// TODO: Add summary/// </summary>public class Calculator{ public int Add(int a, int b) { return a + b; } public int Divide(int numerator, int denominator) { if (denominator == 0) throw new ArgumentException("Cannot divide by zero"); return numerator / denominator; }}
Code fixes support Fix All in Document, Project, or Solution scope. Right-click the light bulb and select “Fix All occurrences in…” to apply fixes to multiple members at once.
Replace the placeholders with meaningful documentation:
Calculator.cs
/// <summary>/// Provides basic arithmetic operations./// </summary>public class Calculator{ /// <summary> /// Adds two integers and returns the result. /// </summary> /// <param name="a">The first number to add.</param> /// <param name="b">The second number to add.</param> /// <returns>The sum of the two numbers.</returns> public int Add(int a, int b) { return a + b; } /// <summary> /// Divides one integer by another. /// </summary> /// <param name="numerator">The number to be divided.</param> /// <param name="denominator">The number to divide by.</param> /// <returns>The quotient of the division.</returns> /// <exception cref="ArgumentException">Thrown when denominator is zero.</exception> public int Divide(int numerator, int denominator) { if (denominator == 0) throw new ArgumentException("Cannot divide by zero"); return numerator / denominator; }}
Notice how CommentSense detected the ArgumentException in the Divide method? Let’s explore this feature:
Validator.cs
/// <summary>/// Validates user input./// </summary>public class Validator{ /// <summary> /// Validates that a string is not null or empty. /// </summary> /// <param name="value">The string to validate.</param> public void ValidateNotEmpty(string value) { if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value)); ArgumentNullException.ThrowIfNull(value); }}
This code will trigger CSENSE012 for the missing <exception> tag. CommentSense scans method bodies for:
Explicit throw statements
Guard clause methods like ArgumentNullException.ThrowIfNull
Optionally, exceptions documented in called methods (when configured)
Apply the code fix to add the missing exception documentation:
Validator.cs
/// <summary>/// Validates user input./// </summary>public class Validator{ /// <summary> /// Validates that a string is not null or empty. /// </summary> /// <param name="value">The string to validate.</param> /// <exception cref="ArgumentNullException">TODO: Add exception description</exception> public void ValidateNotEmpty(string value) { if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value)); ArgumentNullException.ThrowIfNull(value); }}
Create an .editorconfig file in your project root to customize CommentSense behavior:
.editorconfig
root = true[*.cs]# Analyze all members down to internal visibilitycomment_sense.visibility_level = internal# Require summaries to be at least 15 characterscomment_sense.min_summary_length = 15# Detect documentation similar to member namescomment_sense.similarity_threshold = 0.8# Add custom low-quality termscomment_sense.low_quality_terms = TODO, TBD, FixMe, WIP# Exclude constants from documentation requirementscomment_sense.exclude_constants = true
See the Configuration Guide for all available options and detailed explanations.