Avoid excessive class coupling
Avoid excessive class coupling
Microsoft docsDescription
This rule measures class coupling by counting the number of unique type references that a type, method, field, property, or event contains. The default coupling threshold is 95 for types and 40 for other symbol kinds, and the thresholds are configurable.
Types, methods, and other symbols that have a high degree of class coupling can be difficult to maintain. It's a good practice to have types, methods, and other symbols that exhibit low coupling and high cohesion.
Cause
A type, method, field, property, or event is coupled with many other types. Compiler-generated types are excluded from this metric.
How to fix violations
To fix this violation, try to redesign the type or method to reduce the number of types to which it's coupled.
Example
#pragma warning disable CA1506
// The code that's violating the rule is on this line.
#pragma warning restore CA1506When to suppress
You can suppress this warning when the type or method is considered maintainable despite its large number of dependencies on other types. You might see false positive warnings from this rule if all of the following apply:
- You're using Visual Studio 2022 version 17.5 or later with an older version of the .NET SDK, that is, .NET 6 or earlier.
- You're using the analyzers from the .NET 6 SDK or an older version of the analyzer packages, such as Microsoft.CodeAnalysis.FxCopAnalyzers.
The false positives are due to a breaking change in the C# compiler. Consider using a newer analyzer that contains the fix for the false positive warnings. Upgrade to Microsoft.CodeAnalysis.NetAnalyzers version 7.0.0-preview1.22464.1 or newer or use the analyzers from the .NET 7 SDK.