C#idiomatic C#
ca1308fundamentals/code-analysis/quality-rules

Code analysis

CA1308: Normalize strings to uppercase

Learn about code analysis rule CA1308: Normalize strings to uppercase

CA1308 Globalization enabled by default in .NET 10: No
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

An operation normalizes a string to lowercase.

Rule description

Strings should be normalized to uppercase. A small group of characters, when they are converted to lowercase, cannot make a round trip. To make a round trip means to convert the characters from one locale to another locale that represents character data differently, and then to accurately retrieve the original characters from the converted characters.

How to fix violations

Change operations that convert strings to lowercase so that the strings are converted to uppercase instead. For example, change String.ToLower(CultureInfo.InvariantCulture) to String.ToUpper(CultureInfo.InvariantCulture).

When to suppress warnings

It's safe to suppress a warning when you're not making security decisions based on the result of the normalization (for example, when you're displaying the result in the UI).

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

csharp
#pragma warning disable CA1308
// The code that's violating the rule is on this line.
#pragma warning restore CA1308

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

ini
[*.{cs,vb}]
dotnet_diagnostic.CA1308.severity = none

For more information, see How to suppress code analysis warnings.

See also