A type, method, field, property, or event is coupled with many other types. Compiler-generated types are excluded from this metric.
Rule descriptionThis 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.
How to fix violationsTo fix this violation, try to redesign the type or method to reduce the number of types to which it's coupled.
When to suppress warningsYou can suppress this warning when the type or method is considered maintainable despite its large number of dependencies on other types.
Note
You might see false positive warnings from this rule if all of the following apply:
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.
Suppress a warningIf you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1506
// The code that's violating the rule is on this line.
#pragma warning restore CA1506
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1506.severity = none
For more information, see How to suppress code analysis warnings.
Configure thresholdYou can configure the threshold at which this rule fires and the kinds of symbols to analyze. The allowable symbol kinds are:
Assembly
Namespace
Type
Method
Field
Event
Property
Create a text file named CodeMetricsConfig.txt.
Add the desired threshold to the text file in the following format:
CA1506(Method): 60
CA1506(Type): 120
In this example, the rule is configured to fire when a method has more than 60 unique type references or a type has more than 120 unique type references. With this configuration file, the rule will continue to flag fields, properties, and events whose class coupling is greater than the default threshold (40).
In the project file, mark the build action of the configuration file as AdditionalFiles. For example:
<ItemGroup>
<AdditionalFiles Include="CodeMetricsConfig.txt" />
</ItemGroup>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4