Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA1305 fxcop for String Interpolation on .NET 4.5 #1105

Closed
saxenark opened this issue Feb 3, 2017 · 2 comments
Closed

CA1305 fxcop for String Interpolation on .NET 4.5 #1105

saxenark opened this issue Feb 3, 2017 · 2 comments

Comments

@saxenark
Copy link

saxenark commented Feb 3, 2017

VS2015

$"Enabled Check for {feature.ToString()}"

This code shows the following warning on Code Analysis

Warning CA1305 Because the behavior of 'string.Format(string, object)' could vary based on the current user's locale settings, replace this call in 'FeatureController.IsFeatureEnabled(FeatureEnum, Dictionary<string, string>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. If the result of 'string.Format(IFormatProvider, string, params object[])' will be displayed to the user, specify 'CultureInfo.CurrentCulture' as the 'IFormatProvider' parameter. Otherwise, if the result will be stored and accessed by software, such as when it is persisted to disk or to a database, specify 'CultureInfo.InvariantCulture'.

Question is, how do I handle this warning in .NET 4.5 while sticking with string interpolation. I know 4.6 does have FormattableString which can help resolve this warning.

@haakoo
Copy link

haakoo commented Apr 10, 2017

I'm also seeing this warning for code where I don't understand why. The following code results in this warning when compiled for .Net Framework 4.6.1

public static class Class1
{
    public static string Method(string someValue)
    {
        return $"A string with {someValue} embedded in it.";
    }
}

Warning CA1305 Because the behavior of 'string.Format(string, object)' could vary based on the current user's locale settings, replace this call in 'Class1.Method(string)' with a call to 'string.Format(IFormatProvider, string, params object[])'. If the result of 'string.Format(IFormatProvider, string, params object[])' will be displayed to the user, specify 'CultureInfo.CurrentCulture' as the 'IFormatProvider' parameter. Otherwise, if the result will be stored and accessed by software, such as when it is persisted to disk or to a database, specify 'CultureInfo.InvariantCulture'. NetFramework

Why get a warning when you embed a string? Is this a bug in the analyser, or am I missing something?

Working minimal example over at haakoo/WarningCA1305Bug.

@mavasani
Copy link
Contributor

This is a dupe of dotnet/roslyn#8884 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants