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
Different ways for Integer to String Conversions in C#
Converting integers to strings is a common operation in C# programming. There are several built-in methods available, each with different use cases and performance characteristics.
Syntax
Following are the main syntaxes for integer to string conversion −
// Using ToString() method
string str = number.ToString();
// Using Convert.ToString() method
string str = Convert.ToString(number);
// Using string interpolation
string str = $"{number}";
// Using string.Format() method
string str = string.Format("{0}", number);
Using ToString() Method
The ToString() method is the most commonly used approach for converting integers to strings. It's called directly on the integer variable −
using System;
class Program {
static void Main() {
int number = 42;
string result = number.ToString();
Console.WriteLine("Integer: " + number);
Console.WriteLine("String: " + result);
Console.WriteLine("Type: " + result.GetType());
}
}
The output of the above code is −
Integer: 42 String: 42 Type: System.String
Using Convert.ToString() Method
The Convert.ToString() method is useful when you need to handle null values safely, as it returns an empty string for null inputs −
using System;
class Program {
static void Main() {
int number = 150;
string result = Convert.ToString(number);
Console.WriteLine("Converted: " + result);
// Convert.ToString() can also handle nullable integers
int? nullableNumber = null;
string nullResult = Convert.ToString(nullableNumber);
Console.WriteLine("Null converted: '" + nullResult + "'");
}
}
The output of the above code is −
Converted: 150 Null converted: ''
Using String Interpolation
String interpolation provides a clean and readable way to convert integers to strings, especially when combining with other text −
using System;
class Program {
static void Main() {
int age = 25;
int score = 95;
string message = $"Age: {age}, Score: {score}";
Console.WriteLine(message);
// Just the number
string ageString = $"{age}";
Console.WriteLine("Age as string: " + ageString);
}
}
The output of the above code is −
Age: 25, Score: 95 Age as string: 25
Using string.Format() Method
The string.Format() method offers more control over formatting and is useful for complex string compositions −
using System;
class Program {
static void Main() {
int value1 = 10;
int value2 = 20;
int value3 = 30;
string result1 = string.Format("{0}", value1);
string result2 = string.Format("Values: {0}, {1}, {2}", value1, value2, value3);
string result3 = string.Format("Formatted: {0:D4}", value1);
Console.WriteLine(result1);
Console.WriteLine(result2);
Console.WriteLine(result3);
}
}
The output of the above code is −
10 Values: 10, 20, 30 Formatted: 0010
Comparison of Methods
| Method | Performance | Null Safety | Best Use Case |
|---|---|---|---|
| ToString() | Fastest | No (throws exception) | Simple conversions |
| Convert.ToString() | Slower | Yes (returns empty string) | Nullable integers |
| String Interpolation | Fast | No | Combining with text |
| string.Format() | Slowest | No | Complex formatting |
Conclusion
C# provides multiple ways to convert integers to strings, with ToString() being the most efficient for simple conversions. Choose Convert.ToString() for null safety, string interpolation for readability, and string.Format() for complex formatting requirements.
