0
0
CsharpHow-ToBeginner · 2 min read

C# How to Convert int to String Easily

In C#, you can convert an int to a string by using the ToString() method like intValue.ToString() or by using Convert.ToString(intValue).
📋

Examples

Input123
Output"123"
Input0
Output"0"
Input-456
Output"-456"
🧠

How to Think About It

To convert an integer to a string, think of turning a number into words you can read or print. You use built-in methods that take the number and give you back its text form, so you can show it or combine it with other text.
📐

Algorithm

1
Get the integer value you want to convert.
2
Call the <code>ToString()</code> method on the integer or use <code>Convert.ToString()</code> with the integer as input.
3
Store or use the resulting string as needed.
💻

Code

csharp
using System;

class Program {
    static void Main() {
        int number = 123;
        string text = number.ToString();
        Console.WriteLine(text);
    }
}
Output
123
🔍

Dry Run

Let's trace converting the integer 123 to a string using ToString()

1

Start with integer

number = 123

2

Convert to string

text = number.ToString() results in "123"

3

Print string

Console prints: 123

StepVariableValue
1number123
2text"123"
3output123
💡

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

Convert.ToString()
csharp
using System;

class Program {
    static void Main() {
        int number = 456;
        string text = Convert.ToString(number);
        Console.WriteLine(text);
    }
}
This method is useful if you want a static method call and can handle null values safely.
String Interpolation
csharp
using System;

class Program {
    static void Main() {
        int number = 789;
        string text = $"{number}";
        Console.WriteLine(text);
    }
}
This method is handy when combining numbers with other text in a readable way.

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.

ApproachTimeSpaceBest For
ToString()O(1)O(1)Simple direct conversion
Convert.ToString()O(1)O(1)Safe conversion, handles nulls
String InterpolationO(1)O(1)Combining numbers with text
💡
Use ToString() for simple and clear int to string conversion in C#.
⚠️
Trying to assign an int directly to a string variable without conversion causes errors.