Skip to content

BRO1404: Access modifier should be declared

Types and members say their accessibility instead of relying on the default. Replaces StyleCop SA1400.

Example

Before

class Cache
{
    int hits;

    void Reset()
    {
        hits = 0;
    }
}

After

internal class Cache
{
    private int hits;

    private void Reset()
    {
        hits = 0;
    }
}

Why

The default accessibility differs by place (internal for a top-level type, private for a member, public in an interface), so a declaration without a modifier makes the reader work it out.

How the fix works

The fix writes the accessibility the declaration has now, so nothing changes in what can use it. It goes before the other modifiers (after attributes), where IDE0036's modifier order wants it.

This rule replaces the SDK's IDE0040, whose fix writes merge conflict markers in multi-targeted projects under dotnet format (reported upstream as dotnet/roslyn#86000; see decisions).

Configuration

[*.cs]
# The SDK's setting, which BRO1404 and BRO1007 follow:
#   for_non_interface_members (or not set): StyleCop's SA1400/SA1205, as described here
#   always: interface members get one too ('public', C# 8 and later)
#   never, omit_if_default: no modifiers are asked for (nothing is reported)
dotnet_style_require_accessibility_modifiers = for_non_interface_members

Not reported

  • Interface members and types nested in interfaces. With always they are, from C# 8; a file also compiled with an older version (a multi-targeted project's net472 build: C# 7.3) keeps the warning but isn't fixed.
  • Explicit interface implementations, static constructors, operators, finalizers and file types, which can't or needn't have one.
  • Partial members (another part may declare it); partial types are BRO1007's.

Compared with StyleCop

Same positions and output.