C# TextInfo

Performance optimization

TextInfo is found in System.Globalization. It provides several methods. For example, it has methods for changing the case of strings. We use and then benchmark its ToLower method.

ToLower TextInfo benchmark
    See "Benchmark" section below for more information.

Iterations:       10000000 iterations were tested.
TextInfo ToLower: 737 ms         [faster]
String ToLower:   938 ms

Example

.NET Framework information

The .NET Framework defines a TextInfo class that encapsulates certain string-level globalization methods. The TextInfo class provides a ToLower method, a ToTitleCase method, and a ToUpper method. This example shows the ToLower method, but the other methods can be used in the same way. It lowercases constant string data in the example program.

Program that uses TextInfo lowercase [C#]

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
	//
	// Access TextInfo virtual property accessors.
	//
	TextInfo textInfo1 = CultureInfo.InvariantCulture.TextInfo;
	TextInfo textInfo2 = CultureInfo.CurrentCulture.TextInfo; // (FAST)

	//
	// Input mixed-cased strings.
	//
	const string value1 = "Sam Allen";
	const string value2 = "SAM ALLEN 012345";
	const string value3 = "sam allen";

	//
	// Use the ToLower method on the TextInfo variables.
	//
	string lower1 = textInfo1.ToLower(value1);
	string lower2 = textInfo1.ToLower(value2);
	string lower3 = textInfo2.ToLower(value3);

	//
	// Write lowercased strings to the screen.
	//
	Console.WriteLine(lower1);
	Console.WriteLine(lower2);
	Console.WriteLine(lower3);
    }
}

Output

sam allen
sam allen 012345
sam allen
Note

Overview. The program text first evaluates the TextInfo virtual property accessors on the InvariantCulture and CurrentCulture properties. You must include the System.Globalization namespace with a using directive or specify the fully qualified name "System.Globalization.CultureInfo" to access these types.

String ToLower

String type

The String type in the C# programming language defines a ToLower method and ToLowerInvariant method that implement the same functionality as the TextInfo members. The ToLower and ToLowerInvariant methods on strings actually call into the TextInfo virtual property when invoked. For this reason, they always carry the overhead of this virtual property access. The string type methods have no difference in result values but are slower in some cases.

ToLower Method

Benchmark

Here we look at a benchmark of the TextInfo ToLower method and the string type's ToLower method. The benchmark shows that you can improve performance by using the CultureInfo.CurrentCulture.TextInfo variable and calling the instance ToLower method on it. This is possible because you can hoist the virtual property access for TextInfo outside of a tight loop.

Note: This optimization is only useful when you are calling ToLower many times.

Benchmark TextInfo used [C#]

TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;

Statements tested in separate loops [C#]

string lower = textInfo.ToLower("SAMDOTNETPERLS");

string lower = "SAMDOTNETPERLS".ToLower();

More TextInfo methods

Programming tip

You can call the ToTitleCase method to uppercase the first letter in each word in a string. You can also call the ToUpper method to uppercase all letters in the string. This site has more information on these TextInfo methods.

ToTitleCase Method

Summary

The C# programming language

We looked at how you can use the TextInfo class in the C# programming language to lowercase strings. We benchmarked the TextInfo code shown in the article and found that a virtual property accessor can be eliminated by using the TextInfo class and storing it in a variable. Next we noted that there are other TextInfo methods such as ToUpper and ToTitleCase that also provide excellent ways to transform string characters.

String Type
.NET