
org.sonar.plugins.csharp.S4058.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonarlint-omnisharp-plugin Show documentation
Show all versions of sonarlint-omnisharp-plugin Show documentation
Code Analyzer based on Omnisharp
Why is this an issue?
Many string operations, the Compare
and Equals
methods in particular, provide an overload that accepts a
StringComparison
enumeration value as a parameter. Calling these overloads and explicitly providing this parameter makes your code
clearer and easier to maintain.
This rule raises an issue when a string comparison operation doesn’t use the overload that takes a StringComparison
parameter.
Noncompliant code example
using System;
namespace MyLibrary
{
public class Foo
{
public bool HaveSameNames(string name1, string name2)
{
return string.Compare(name1, name2) == 0; // Noncompliant
}
}
}
Compliant solution
using System;
namespace MyLibrary
{
public class Foo
{
public bool HaveSameNames(string name1, string name2)
{
return string.Compare(name1, name2, StringComparison.OrdinalIgnoreCase) == 0;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy