BRO1002: Single-line comments should begin with a space¶
A // comment should have a space between the slashes and the text. Replaces StyleCop SA1005.
Example¶
Before¶
public class Invoice
{
//Total including VAT.
public decimal Total { get; set; }
public decimal Net() => Total / 1.25m; //VAT is 25%
}
After¶
public class Invoice
{
// Total including VAT.
public decimal Total { get; set; }
public decimal Net() => Total / 1.25m; // VAT is 25%
}
Why¶
Comments read like prose, and // note is how almost all C# code writes them. Consistent spacing also makes
commented-out code (////) and separator lines (//----) stand out from real comments.
How the fix works¶
The fix inserts a space. Tabs and other whitespace after // become a single space, and a comment with nothing but
whitespace becomes //.
Not reported¶
- Comments that already start with whitespace, including
// two spacesfor indented text. - An empty comment (
//). ///and////, which are used for documentation and commented-out code.- Separator lines starting with
//--, such as//-----------. dotnet newtemplate markers starting with//-:or//+:(//-:cnd:noEmit), which a space would break.- Text in inactive
#ifblocks, which isn't a comment to the compiler.
Compared with StyleCop¶
Same results as StyleCop 1.2. StyleCop 1.1.118 also reported // two spaces. For a comment with only whitespace,
StyleCop's fix leaves a trailing space (//); BRO1002 writes //. StyleCop reports the template markers //-: and
//+: and its fix breaks them (#2689, open).