org.sonar.plugins.csharp.S4058.html Maven / Gradle / Ivy
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 - 2024 Weber Informatics LLC | Privacy Policy