Updated on

To convert an int to a string in C#, we call ToString() on it: luckyNumber.ToString() turns the number 3 into the text "3". It is the most direct option and, in our .NET 10 benchmark, one of the fastest.

int is an alias of the Int32 type, which represents a whole number, while string represents a sequence of characters, simply put, text. When we want to show a number stored in an int variable on the screen, we first need to turn it into text. We can do that explicitly, or let C# do it for us.

C# gives us at least eight ways to get there, and they differ in speed. Let’s go through each one, measure them, and look at the one case where the default ToString() can surprise us: negative numbers in some cultures.

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

Using Int32.ToString Method to Convert Int to String

Let’s first create a console application, add using System.Globalization; and using System.Text; at the top of Program.cs, and define an int variable with a value of 3:

var luckyNumber = 3;

Next, let’s use this variable in our examples to convert int to string using the different methods and display it on the console window.

The Int32.ToString method is the preferred method to convert int to string:

Console.WriteLine(luckyNumber.ToString());

We convert the value of the luckyNumber variable using ToString method and display the string to the console window.

Console.WriteLine() has an overload that takes an int directly and turns it into text for us, so we can also skip ToString() and pass the variable as it is:

Console.WriteLine(luckyNumber);

Using Convert.ToString Method To Convert Int to String

We can convert the value of the luckyNumber variable using the Convert.ToString method and display the string to the console window:

Console.WriteLine(Convert.ToString(luckyNumber));

For an int, Convert.ToString() does nothing more than call ToString() on the value, so the two always give the same result. It helps when a value typed as object might be null: Convert.ToString() returns an empty string, while calling ToString() on a null reference throws a NullReferenceException.

Convert Int to String Using the String.Format Method

We use this method when we want to apply formatting on a number:

Console.WriteLine(string.Format("This is our number {0}", luckyNumber));

Therefore, we use the luckyNumber variable as a parameter for the string.Format method, which converts the value into a string and provides a format to display it to the console window.

string.Format() also accepts format strings such as {0:N0} for thousands separators or {0:D5} for leading zeros, and our guide to numeric format strings covers them all.

Using String Interpolation

String interpolation is very similar to the previous string.Format() method, but more readable. It arrived in C# 6:

Console.WriteLine($"This is our number {luckyNumber}");

We put the luckyNumber variable inside curly braces and start the string with the $ sign, which converts the value into a string. Our article on string interpolation in C# goes further into what we can put inside those braces.

Using Concatenation With Empty String

When we concatenate text with numbers, C# converts the numbers into text for us. We can execute the concatenation using several different methods:

Console.WriteLine(string.Concat(string.Empty, luckyNumber));

In our case, the String.Concat method concatenates an empty string with the luckyNumber variable, which converts the value into a string and displays the text “3” to the console window.

Next, we can use String.Join to achieve the same thing:

Console.WriteLine(string.Join(string.Empty, luckyNumber));

Additionally, let’s see how we can do the same with the + sign:

Console.WriteLine(string.Empty + luckyNumber);

When we use the + sign, we need to watch the order. C# evaluates + from left to right, so if two numbers come before the first string, it adds them first and only then converts the sum to text:

Console.WriteLine(luckyNumber + luckyNumber + string.Empty + luckyNumber + luckyNumber);

Here, the result will be “633”.

Using StringBuilder().Append Method

When we build one string out of many pieces, a StringBuilder saves memory allocations, and its Append() method accepts an int directly:

Console.WriteLine(new StringBuilder().Append(luckyNumber).ToString());

This method adds the string representation of the luckyNumber object to the StringBuilder instance. Then with the ToString method, we convert the StringBuilder object into string, and we display the text “3” to the console window.

Which Way to Convert Int to String Is the Fastest?

To understand the difference in performance between all of the above methods, we benchmark them with BenchmarkDotNet. We convert two numbers with every method: our luckyNumber, which is 3, and 1,234,567. The reason for the second number shows up in the results:

| Method              | Number  | Mean      | Allocated |
|-------------------- |-------- |----------:|----------:|
| IntToString         | 3       |  1.610 ns |         - |
| ConvertToString     | 3       |  1.615 ns |         - |
| StringFormat        | 3       | 35.537 ns |      48 B |
| StringInterpolation | 3       | 25.937 ns |      24 B |
| StringConcat        | 3       |  1.608 ns |         - |
| StringJoin          | 3       |  8.886 ns |      24 B |
| PlusSign            | 3       |  1.721 ns |         - |
| StringBuilderAppend | 3       | 20.457 ns |     128 B |
|                     |         |           |           |
| IntToString         | 1234567 | 10.453 ns |      40 B |
| ConvertToString     | 1234567 | 11.046 ns |      40 B |
| StringFormat        | 1234567 | 40.581 ns |      64 B |
| StringInterpolation | 1234567 | 29.754 ns |      40 B |
| StringConcat        | 1234567 | 11.431 ns |      40 B |
| StringJoin          | 1234567 | 18.587 ns |      64 B |
| PlusSign            | 1234567 | 11.851 ns |      40 B |
| StringBuilderAppend | 1234567 | 25.885 ns |     144 B |

Four methods share first place: ToString(), Convert.ToString(), string.Concat() and the + sign. For the number 3, none of them allocates anything. Since .NET 8, ToString() keeps a cache of the strings for 0 to 299 and hands back the same instance every time, so converting 3 a million times creates no new strings. Convert.ToString() calls ToString() internally, and the two concatenations end up there as well, so all four benefit.

With 1,234,567 the cache no longer applies, and every method allocates at least the result string. The same four stay in front, at around 10 to 12 nanoseconds each on our machine.

The other four do extra work on every call. string.Format() parses its format string, string.Join() boxes the number to pass it as a list of values, string interpolation fills a temporary buffer and copies it out, and StringBuilder creates the builder object first. They allocate even for 3.

Mostly in our applications, we do a few conversions at a time, and for a small number of conversions we could use any method that fits the context. The difference is at most a few dozen nanoseconds per call.

For code that converts a lot of numbers, the native methods ToString() and Convert.ToString() are among the fastest.

Does int.ToString() Depend on the Current Culture?

Yes, but for a whole number in the default format, the only thing the culture can change is the negative sign.

Positive numbers come out the same everywhere. 1234.ToString() is "1234" whatever culture the machine runs.

Negative numbers can differ. On .NET 10, Swedish, Finnish and Norwegian Bokmål use the Unicode minus sign, U+2212, instead of the ASCII hyphen. So (-5).ToString() on a Swedish machine returns "−5". It looks similar on screen, but it is a different character: comparing it with "-5" returns false, and parsing it with CultureInfo.InvariantCulture fails.

String interpolation and the + sign use the current culture as well, so they behave the same way.

For text a person reads, the default is right. For text another program reads, such as a file, a URL, a cache key or JSON, we pass CultureInfo.InvariantCulture.

Microsoft Learn’s Int32.ToString reference says the parameterless overload “formats an Int32 value in the default (“G”, or general) format by using the NumberFormatInfo object of the current culture.” The current culture is whatever the user’s machine is set to, so the same line of code can produce different text on two machines:

var negativeNumber = -5;
var swedish = CultureInfo.GetCultureInfo("sv-SE");

Console.WriteLine(negativeNumber.ToString(swedish) == "-5");
Console.WriteLine(negativeNumber.ToString(CultureInfo.InvariantCulture) == "-5");

The first line prints False and the second prints True. Passing CultureInfo.InvariantCulture takes one extra argument, and we should add it anywhere the text leaves our program.

Which Way to Convert Int to String Should We Use?

For a plain conversion, ToString(). It says exactly what it does, and it shares first place in our benchmark.

When the number goes inside a longer piece of text, string interpolation is the better choice. $"This is our number {luckyNumber}" is easier to read than string.Format() with numbered placeholders. It is slower than a bare ToString(), but that only matters inside a hot loop.

string.Format() still has one job interpolation cannot do: it takes the format string at runtime, for example from a resource file with a translated sentence.

Convert.ToString() is the pick when the value arrives as an object that might be null, because it returns an empty string where ToString() would throw.

string.Concat(), string.Join() and StringBuilder all work, but they are tools for joining many values. Using them for a single number only adds noise.

Text that another program will read gets ToString(CultureInfo.InvariantCulture), so a Swedish or Finnish machine produces the same minus sign as ours.

Conclusion

Converting an int into a string is a task we do all the time. In this article, we’ve learned eight ways to do it, their differences, which ones perform best on .NET 10, and why text meant for another program should use CultureInfo.InvariantCulture.

When we need to go the other way, our article on converting a string to an int picks up from here.

Tested with .NET 10.0.10 (SDK 10.0.302), BenchmarkDotNet 0.15.8 and MSTest 4.5.1.