Convert an integer to a string in C#
This post will discuss how to convert an integer to a string in C#.
1. Using Int32.ToString() method
A simple and fairly efficient solution is to call the ToString() method on the integer instance to convert it into its equivalent string representation.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int i = 10; string str = i.ToString(); Console.WriteLine(str); } } |
2. Using Convert.ToString() method
The Convert.ToString() method is used to convert the specified value to its equivalent string representation.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int i = 10; string str = Convert.ToString(i); Console.WriteLine(str); } } |
3. Using String.Format() method
We can use String.Format() method to insert an integer’s value into another string, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int i = 10; string str = String.Format("{0}", i); Console.WriteLine(str); } } |
4. String Interpolation
Starting with C# 6, one can use String interpolation ($), which provides a convenient syntax for converting an integer to a string.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int i = 10; string str = $"{i}" Console.WriteLine(str); } } |
5. Concatenation with String.Empty() method
String concatenation in C# is done using the + operator. To convert an integer to a string, simply concat the integer with an empty string.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int i = 10; string str = i + String.Empty; Console.WriteLine(str); } } |
6. Using StringBuilder.Append() method
The StringBuilder.Append() method can be used to append the string representation of the specified integer to the current instance.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.Text; public class Example { public static void Main() { int i = 10; string str = new StringBuilder().Append(i).ToString(); Console.WriteLine(str); } } |
7. Using String.Concat() method
We can use String.Concat() method to create the string representation of the specified integer.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.Text; public class Example { public static void Main() { int i = 10; string str = String.Concat(i); Console.WriteLine(str); } } |
8. Using String.Join() method
The following code example demonstrates how we can use the String.Join() method for generating a string from an integer using an empty string as a separator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.Text; public class Example { public static void Main() { int i = 10; string str = String.Join(String.Empty, i); Console.WriteLine(str); } } |
That’s all about converting an integer to a string in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)