In this article, we are going to talk about how to convert int to string in C#.
Int
is an alias of the Int32
type, which represents a whole number, while string
representing a collection of characters, simply defined as text. When we want to show a number on the screen, which is stored in a variable of type int
, first we need to convert the number into text. In addition, we can convert the value explicitly or let the interpreter use the default method for conversion.
Let’s start.
Using Int32.ToString Method to Convert Int to String
Let’s first create a console application, 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.
Since the Console.WriteLine
method calls the ToString
method by default on the input object parameter different than string, converting a string value using this method can be done even if we omit the ToString
method. We can let the compiler use the default method to convert the value of the luckyNumber
variable and display the string to the console window without explicit conversion:
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));
Convert Int to String Using the String.Format Method
We use this method when we want to apply formating 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.
Using String Interpolation
String interpolation is very similar to the previous string.Format
method, more readable and introduced later in C#:
Console.WriteLine($"This is our number: {luckyNumber}");
We use the luckyNumber
variable surrounded by brackets and starting with $
sign, which converts the value into a string
.
Using Concatenation With Empty String
When we concatenate text with numbers, the compiler 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 using +
sign for concatenation, we need to be aware that if we start with two or more numbers, the compiler will calculate the sum and then convert the result to string
:
Console.WriteLine(luckyNumber + luckyNumber + string.Empty + luckyNumber + luckyNumber);
Here, the result will be “633”.
Using StringBuilder().Append Method
When we concatenate many strings together, considering the amount of memory allocation, we use the StringBuilder().Append
method:
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.
Benchmark
In order to understand the difference in performance between all of the above methods, we have created two benchmarks. We measured the time of execution of every different conversion, repeated in a for-loop 200 000 times for the first benchmark, and 2 million times for the second benchmark:
Benchmarks with 200,000 repetitions: | Method | Speed (ms)| |------------------------|-----------| | 'ToString' | 2| | 'ConvertToString' | 3| | 'String.Format' | 62| | 'String Interpolation' | 68| | 'String.Concat' | 56| | 'String.Join' | 77| | 'Plus sign' | 17| | 'Stringbuilder Append' | 79| Benchmarks with 2,000,000 repetitions: | Method | Speed (ms)| |------------------------|-----------| | 'ToString' | 38| | 'ConvertToString' | 36| | 'String.Format' | 381| | 'String Interpolation' | 278| | 'String.Concat' | 73| | 'String.Join' | 118| | 'Plus sign' | 29| | 'Stringbuilder Append' | 167|
As we can see for both benchmarks the ToString
and ConvertToString
methods alongside the +
sigh concatenation, have the best results.
Mostly in our applications, we do a few conversions at a time. And for the small number of conversions, we could use any method that fits the context. Using one method over another does not show any difference in performance.
For applications that can have a lot of conversions, it is advised to use the native methods .ToString
or Convert.ToString
which are performing the best.
Conclusion
Converting int
into string
is a common task that we do often. In this article, we’ve learned about different methods for conversion. their differences, and which conversions show the best performance.