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
Represent Int32 as a String in C#
The Int32 type in C# represents a 32-bit signed integer. To convert an Int32 value to its string representation, you can use the ToString() method. This method converts the numeric value into a readable string format.
Syntax
Following is the syntax for converting an Int32 to string using ToString() −
int number = value; string result = number.ToString();
You can also use format specifiers with ToString() −
string result = number.ToString("format");
Using ToString() Method
Basic Conversion Example
The simplest way to convert an Int32 to string is using the parameterless ToString() method −
using System;
class Demo {
static void Main() {
int val = 1023;
string result = val.ToString();
Console.WriteLine("Integer converted to string = " + result);
Console.WriteLine("Type of result: " + result.GetType().Name);
}
}
The output of the above code is −
Integer converted to string = 1023 Type of result: String
Using Format Specifiers
Example with Different Formats
You can format the Int32 value using various format specifiers when converting to string −
using System;
class FormatDemo {
static void Main() {
int number = 12345;
Console.WriteLine("Default: " + number.ToString());
Console.WriteLine("Currency: " + number.ToString("C"));
Console.WriteLine("Decimal: " + number.ToString("D8"));
Console.WriteLine("Hexadecimal: " + number.ToString("X"));
Console.WriteLine("Number with commas: " + number.ToString("N"));
}
}
The output of the above code is −
Default: 12345 Currency: $12,345.00 Decimal: 00012345 Hexadecimal: 3039 Number with commas: 12,345.00
Alternative Conversion Methods
Using Convert.ToString() and String Interpolation
Besides ToString(), there are other ways to convert Int32 to string −
using System;
class AlternativeMethods {
static void Main() {
int value = 789;
// Using ToString()
string method1 = value.ToString();
// Using Convert.ToString()
string method2 = Convert.ToString(value);
// Using string interpolation
string method3 = $"{value}";
// Using string concatenation
string method4 = "" + value;
Console.WriteLine("ToString(): " + method1);
Console.WriteLine("Convert.ToString(): " + method2);
Console.WriteLine("String interpolation: " + method3);
Console.WriteLine("String concatenation: " + method4);
}
}
The output of the above code is −
ToString(): 789 Convert.ToString(): 789 String interpolation: 789 String concatenation: 789
Comparison of Conversion Methods
| Method | Performance | Null Handling | Format Support |
|---|---|---|---|
| ToString() | Fast | Throws exception if null | Yes |
| Convert.ToString() | Slightly slower | Returns empty string if null | Limited |
| String interpolation | Fast | Handles null gracefully | Yes (with format specifiers) |
| String concatenation | Slower for multiple values | Converts null to "null" | No |
Conclusion
The ToString() method is the most common and efficient way to convert an Int32 to string in C#. It supports various format specifiers for customized output and provides excellent performance for most scenarios.
