C# How to Convert int to String Easily
ToString() method like intValue.ToString() or by using Convert.ToString(intValue).Examples
How to Think About It
Algorithm
Code
using System; class Program { static void Main() { int number = 123; string text = number.ToString(); Console.WriteLine(text); } }
Dry Run
Let's trace converting the integer 123 to a string using ToString()
Start with integer
number = 123
Convert to string
text = number.ToString() results in "123"
Print string
Console prints: 123
| Step | Variable | Value |
|---|---|---|
| 1 | number | 123 |
| 2 | text | "123" |
| 3 | output | 123 |
Why This Works
Step 1: Using ToString() method
The ToString() method is built into all numbers in C# and returns the text form of the number.
Step 2: Alternative Convert.ToString()
The Convert.ToString() method also turns numbers into strings and can handle nulls safely.
Step 3: Result is a string
After conversion, the number is now text and can be used wherever strings are needed, like printing or joining with other text.
Alternative Approaches
using System; class Program { static void Main() { int number = 456; string text = Convert.ToString(number); Console.WriteLine(text); } }
using System; class Program { static void Main() { int number = 789; string text = $"{number}"; Console.WriteLine(text); } }
Complexity: O(1) time, O(1) space
Time Complexity
Converting an int to string is a constant time operation because it involves a fixed number of steps regardless of the integer's size.
Space Complexity
The space used is constant, only enough to store the resulting string representation of the integer.
Which Approach is Fastest?
All methods like ToString(), Convert.ToString(), and string interpolation have similar performance for this simple conversion.
| Approach | Time | Space | Best For |
|---|---|---|---|
| ToString() | O(1) | O(1) | Simple direct conversion |
| Convert.ToString() | O(1) | O(1) | Safe conversion, handles nulls |
| String Interpolation | O(1) | O(1) | Combining numbers with text |
ToString() for simple and clear int to string conversion in C#.