rspec/rules/S4056/csharp/rule.adoc

71 lines
2.2 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
When a ``++System.Globalization.CultureInfo++`` or ``++IFormatProvider++`` object is not supplied, the default value that is supplied by the overloaded member might not have the effect that you want in all locales.
You should supply culture-specific information according to the following guidelines:
* If the value will be displayed to the user, use the current culture. See ``++CultureInfo.CurrentCulture++``.
* If the value will be stored and accessed by software (persisted to a file or database), use the invariant culture. See ``++CultureInfo.InvariantCulture++``.
* If you do not know the destination of the value, have the data consumer or provider specify the culture.
This rule raises an issue when a method or constructor calls one or more members that have overloads that accept a ``++System.IFormatProvider++`` parameter, and the method or constructor does not call the overload that takes the ``++IFormatProvider++`` parameter. This rule ignores calls to .NET Framework methods that are documented as ignoring the ``++IFormatProvider++`` parameter as well as the following methods:
* ``++Activator.CreateInstance++``
* ``++ResourceManager.GetObject++``
* ``++ResourceManager.GetString++``
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
using System;
namespace MyLibrary
{
public class Foo
{
public void Bar(String string1)
{
if(string.Compare(string1, string2, false) == 0) // Noncompliant
{
Console.WriteLine(string3.ToLower()); // Noncompliant
}
}
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
using System;
using System.Globalization;
namespace MyLibrary
{
public class Foo
{
public void Bar(String string1, String string2, String string3)
{
if(string.Compare(string1, string2, false,
CultureInfo.InvariantCulture) == 0)
{
Console.WriteLine(string3.ToLower(CultureInfo.CurrentCulture));
}
}
}
}
----
2021-04-28 16:49:39 +02:00
== Exceptions
This rule will not raise an issue when the overload is marked as obsolete.
ifdef::env-github,rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]