Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Int to String Conversion: Methods and Pitfalls

Understand C# int-to-string conversion: ToString, Convert.ToString, string interpolation, culture-aware formatting, and common pitfalls to avoid.

~6 min readC#
#C##.NET#ToString#string formatting
Diagram illustrating C# int to string conversion methods with culture and formatting options.

Converting an int to a string is a common operation in C#. The language offers several ways to perform this conversion, and each approach has subtle differences in behavior, formatting, and culture handling. This article covers the main methods for int-to-string conversion, when to use each, and the pitfalls that can trip up even experienced developers.

The Simplest Way: ToString()

The most direct approach is to call the ToString() method on the integer instance. The int type is an alias for System.Int32, whose ToString() implementation returns the numeric value as a string.

csharp
int number = 42; string text = number.ToString(); Console.WriteLine(text); // Output: 42

The parameterless ToString() uses the current culture's negative sign convention. For a plain integer without a format specifier, the result is the digits and, if negative, the culture's minus sign. Thousands separators are not added under the general format.

If you need a specific numeric format, pass a format string as an argument. For example, number.ToString("D4") produces a four-digit zero-padded string like 0042. The format specifier follows the standard .NET numeric format strings, giving you control over padding, thousands separators, and scientific notation.

Convert.ToString and Its Differences

Convert.ToString is an alternative that behaves like int.ToString() for an integer value and adds null handling when it is used on an object. When you pass an int, it delegates to the integer's ToString() implementation. The practical difference appears when you use it on an object that might be null: Convert.ToString returns String.Empty for null, whereas calling object.ToString() directly throws a NullReferenceException.

csharp
object obj = null; string result = Convert.ToString(obj); // Returns ""

For a non-null int, both approaches are equivalent. If you are writing generic code that handles multiple types, Convert.ToString is safer because it avoids null exceptions. It also supports culture-sensitive conversion through an overload that accepts an IFormatProvider.

csharp
int number = 1234; string text = Convert.ToString(number, CultureInfo.InvariantCulture);

Use Convert.ToString when you are working with object types or when you need a uniform conversion that handles null gracefully. For a known int variable, ToString() is more idiomatic.

String Interpolation and Format Strings

String interpolation, introduced in C# 6, is a concise way to embed an integer directly into a string. The compiler turns interpolation into the appropriate string-formatting code. On modern .NET, an interpolated string handler can be used to avoid boxing and reduce allocations; on older targets, the code typically calls string.Format. The syntax is more readable either way.

csharp
int age = 30; string message = $"User is {age} years old.";

Interpolation also supports format specifiers inside the braces. For example, {age:D5} pads the number to five digits. This is especially useful when you need to combine text and numbers without multiple concatenation operations.

csharp
int total = 1999; string output = $"Total: {total:C0}"; // Currency format with no decimal places

For integer-to-string conversion, interpolation is often the clearest choice when the number is part of a larger message. It avoids the clutter of string.Format placeholders and reduces the chance of mismatched arguments.

Culture and Localization in Conversion

One of the most overlooked aspects of int-to-string conversion is culture. The ToString() and Convert.ToString methods use the current culture by default, which affects negative signs and, when a format with separators is used, digit grouping. Some cultures also use native digit shapes in formatted output; for example, ar-SA may use Arabic-Indic digits instead of Western digits.

If you are producing output that will be parsed back by another system or stored in a file, use CultureInfo.InvariantCulture to ensure consistency across environments.

csharp
int number = -1234; string text = number.ToString(CultureInfo.InvariantCulture);

The invariant culture is based on the English culture but with no regional variations. It uses the hyphen for negative numbers and does not insert thousands separators unless you explicitly request them. This is the safest choice for serialization, logging, and any scenario where the string will be consumed by software rather than a human reader.

When you do want localized formatting, pass a specific culture. For instance, number.ToString("N0", new CultureInfo("de-DE")) formats the number with German thousands separator conventions. This is useful for user-facing displays, but be aware that the same string may not be parseable by int.Parse without specifying the same culture.

Performance Considerations

Performance is rarely a concern for a single conversion, but it becomes relevant when converting many integers in a loop or in a high-throughput service. The primary cost is memory allocation: each conversion creates a new string object. The CPU cost is relatively low, but the allocation pressure can affect garbage collection.

All the methods discussed—ToString, Convert.ToString, and interpolation—allocate a new string. There is no way to avoid that allocation if you need a string. However, you can reduce overhead by avoiding redundant conversions. For example, if you are building a large string from many numbers, use StringBuilder with Append and pass the integer directly; the StringBuilder internally converts the number to a string but avoids intermediate concatenation allocations.

csharp
StringBuilder sb = new StringBuilder(); for (int i = 0; i < 1000; i++) { sb.Append(i); } string result = sb.ToString();

In terms of method choice, int.ToString() is the most direct and usually has the least overhead. Convert.ToString may go through an extra conversion or null check when called on an object, though the difference is negligible outside tight loops. Interpolation is convenient. Depending on the target, it compiles to a string.Format call or, on modern .NET, uses a DefaultInterpolatedStringHandler that avoids boxing and reduces allocations. For a single conversion, the differences are negligible.

If you are writing performance-sensitive code, measure the actual impact rather than assuming one method is faster. The JIT compiler may inline some conversions, but the larger cost is usually the string allocation itself, which is unavoidable.

Common Pitfalls and How to Avoid Them

A frequent mistake is assuming that ToString() always produces the same result across cultures. As noted, the default uses the current culture, so a negative number might be formatted with a different minus sign in some locales. Always pass CultureInfo.InvariantCulture when the output is meant for machine consumption.

Another pitfall is using format specifiers incorrectly. For example, number.ToString("C") on an integer will produce a currency string, but the result depends on the culture's currency symbol and decimal places. If you intended a plain number, use "G" or no format at all.

Interpolation can also cause subtle issues if you forget to escape braces. In an interpolated string, {{ and }} are used to output literal braces, which can be confusing when mixing with format specifiers. Always test the output when the format string contains braces.

Finally, be careful when converting a string back to an int. The string produced by ToString() with a specific culture may not parse correctly with int.Parse under a different culture. Use the same culture for both operations, or use int.Parse with NumberStyles.Integer and the invariant culture. If the string includes group separators, include NumberStyles.AllowThousands as well.

Choosing the Right Method

The choice between ToString, Convert.ToString, and interpolation depends on the context. Use int.ToString() when you have a known integer and need a simple, direct conversion. Use Convert.ToString when you are handling object types or want null safety. Use string interpolation when you are building a string that combines text and numbers, as it improves readability and reduces the chance of argument mismatch.

For culture-sensitive output, always specify the culture explicitly. The default culture is convenient for local user interfaces, but it is a source of bugs in serialization and logging. The invariant culture is the standard for any data that crosses system boundaries.

When performance is critical, prefer ToString over Convert.ToString and avoid unnecessary conversions. If you are building a large string, use StringBuilder and append the integer directly. Interpolation is optimized in modern .NET, but it still creates a string; the main overhead is the same as any other method.

Understanding these nuances ensures that your C# conversion code is correct, maintainable, and performs well in production.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.