The following example shows a method that incorrectly compares two string values using an invariant comparison.
[C#]
using System;
namespace Samples
{
class Program
{
static void Main(string[] args)
{
if (string.Equals("ß", "SS", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("The values are equal!");
}
else
{
Console.WriteLine("The values are not equal!");
}
}
}
}
The above outputs the following:
The values are equal!
The two values are considered equal because linguisitic casing is being taken into consideration.
To fix the above violation, replace the invariant comparison with an ordinal comparison.
The following example shows this.
[C#]
using System;
namespace Samples
{
class Program
{
static void Main(string[] args)
{
if (string.Equals("ß", "SS", StringComparison.OrdinalCultureIgnoreCase))
{
Console.WriteLine("The values are equal!");
}
else
{
Console.WriteLine("The values are not equal!");
}
}
}
}
The above outputs the following:
The values are not equal!