Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Convert Integer to String
To convert an integer to string in C#, you can use several methods. The most common approach is the ToString() method, which provides a simple and direct way to convert any integer value to its string representation.
Syntax
Following is the syntax for using ToString() method −
string result = number.ToString();
Alternative syntax using Convert.ToString() method −
string result = Convert.ToString(number);
Using ToString() Method
The ToString() method is called on the integer variable to convert it to a string −
using System;
class Program {
static void Main(string[] args) {
int num = 299;
string s = num.ToString();
Console.WriteLine("Original integer: " + num);
Console.WriteLine("Converted string: " + s);
Console.WriteLine("Type of s: " + s.GetType());
}
}
The output of the above code is −
Original integer: 299 Converted string: 299 Type of s: System.String
Using Convert.ToString() Method
The Convert.ToString() method is another way to convert integers to strings and can handle null values safely −
using System;
class Program {
static void Main(string[] args) {
int num1 = 12345;
int num2 = -987;
string str1 = Convert.ToString(num1);
string str2 = Convert.ToString(num2);
Console.WriteLine("Positive number: " + str1);
Console.WriteLine("Negative number: " + str2);
}
}
The output of the above code is −
Positive number: 12345 Negative number: -987
Using String Interpolation
You can also convert integers to strings using string interpolation, which is convenient for formatting −
using System;
class Program {
static void Main(string[] args) {
int score = 95;
int total = 100;
string result1 = $"{score}";
string result2 = $"Score: {score}/{total}";
Console.WriteLine("Simple conversion: " + result1);
Console.WriteLine("Formatted string: " + result2);
}
}
The output of the above code is −
Simple conversion: 95 Formatted string: Score: 95/100
Comparison of Methods
| Method | Performance | Null Handling | Use Case |
|---|---|---|---|
| ToString() | Fast | Throws exception on null | Direct conversion of known values |
| Convert.ToString() | Slightly slower | Returns empty string for null | Safe conversion with potential null values |
| String Interpolation | Fast | Handles null gracefully | Formatting and combining with other text |
Conclusion
Converting integers to strings in C# can be accomplished using ToString(), Convert.ToString(), or string interpolation. The ToString() method is the most commonly used approach for direct conversion, while Convert.ToString() provides better null safety.
