SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
C#

C# static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your C# code

  • All rules 493
  • Vulnerability46
  • Bug88
  • Security Hotspot24
  • Code Smell335

  • Quick Fix 61
Filtered: 30 rules found
confusing
    Impact
      Clean code attribute
        1. "Trace.WriteLineIf" should not be used with "TraceSwitch" levels

           Code Smell
        2. Generic logger injection should match enclosing type

           Code Smell
        3. Extensions should be in separate namespaces

           Code Smell
        4. Methods should not have identical implementations

           Code Smell
        5. Property names should not match get methods

           Code Smell
        6. Method signatures should not contain nested generic types

           Code Smell
        7. Collection sizes and array length comparisons should make sense

           Bug
        8. A conditionally executed single line should be denoted by indentation

           Code Smell
        9. Objects should not be disposed more than once

           Code Smell
        10. Parameter names should not duplicate the names of their methods

           Code Smell
        11. "params" should not be introduced on overrides

           Code Smell
        12. Composite format strings should be used correctly

           Code Smell
        13. Exceptions should not be explicitly rethrown

           Code Smell
        14. "abstract" classes should not have "public" constructors

           Code Smell
        15. Variables should not be checked against the values they're about to be assigned

           Code Smell
        16. Loggers should be named for their enclosing types

           Code Smell
        17. Methods should not return constants

           Code Smell
        18. "private" methods called only by inner classes should be moved to those classes

           Code Smell
        19. Ternary operators should not be nested

           Code Smell
        20. "params" should be used on overrides

           Code Smell
        21. "GC.SuppressFinalize" should not be invoked for types without destructors

           Code Smell
        22. Types should not have members with visibility set higher than the type's visibility

           Code Smell
        23. Fields that are only assigned in the constructor should be "readonly"

           Code Smell
        24. Methods without side effects should not have their return values ignored

           Bug
        25. Test classes should contain at least one test case

           Code Smell
        26. "sealed" classes should not have "protected" members

           Code Smell
        27. "for" loop increment clauses should modify the loops' counters

           Code Smell
        28. Loops with at most one iteration should be refactored

           Bug
        29. Tests should not be ignored

           Code Smell
        30. Redundant pairs of parentheses should be removed

           Code Smell

        Methods without side effects should not have their return values ignored

        intentionality - logical
        reliability
        Bug
        • suspicious
        • confusing

        Why is this an issue?

        How can I fix it?

        More Info

        When you do not use the return value of a method with no side effects, it indicates that something is wrong. Either this method is unnecessary, or the source code does not behave as expected and could lead to code defects. For example, there are methods, such as DateTime.AddYears, that don’t change the value of the input object, but instead, they return a new object whose value is the result of this operation, and as a result that you will have unexpected effects if you do not use the return value.

        This rule raises an issue when the results of the following methods are ignored:

        • LINQ
        • Pure methods
        • Any method on build-in types
        • Any method on Immutable collections

        Special cases:

        • Although string.Intern has a side effect, ignoring its return value is still suspicious as it is the only reference ensured to point to the intern pool.
        • LINQ methods can have side effects if they are misused. For example:
        data.All(x =>
        {
            x.Property = "foo";
            return true;
        });
        

        Such code should be rewritten as a loop because Enumerable.All<TSource> method should be used to determine if all elements satisfy a condition and not to change their state.

        Exceptions

        This rule doesn’t report issues on invocations with out or ref arguments.

          Available In:
        • SonarQube IdeCatch issues on the fly,
          in your IDE
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
        • SonarQube Community BuildAnalyze code in your
          on-premise CI
          Available Since
          9.1
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          9.1

        © 2008-2025 SonarSource SA. All rights reserved.

        Sonar helps developers write Clean Code.
        Privacy Policy | Cookie Policy | Terms of Use