HOWTO · Csharp
How to Convert Int to String in C#
This C# int to string how-to article shows different methods to convert an integer to string in C#. It introduces methods like ToString method, Convert.ToString method, string formatting, and string concatenation method.
On this page
C# has different methods to convert int to string.
This article introduces methods like the ToString method, Convert.ToString method, string formatting, and StringBuilder method.
C# int to string Conversion - Int16.ToString() / Int32.ToString() / Int64.ToString() Method
The ToString() method of the Int16/32/64 data type converts the integer to the string representation and is mainly for the purpose of the display.
using System;
public class Demo {
public static void Main() {
// Your code here!
int num = 80;
string numString = num.ToString();
System.Console.WriteLine(numString);
}
}
Output:
80
C# int to string Conversion - Convert.ToString() Method
Convert class in the System namespace converts a data type to another data type. Convert.ToString() method converts the given value to its string representation.
using System;
public class Demo {
public static void Main() {
// Your code here!
int num = 80;
string numString = Convert.ToString(num);
System.Console.WriteLine(numString);
}
}
Output:
80
C# int to string Conversion - String.Format() Method
The String.Format method converts the given objects to strings by following the specified formats.
using System;
public class Demo {
public static void Main() {
// Your code here!
int num = 80;
string numString = string.Format("{0}", num);
System.Console.WriteLine(numString);
}
}
Here, {0} is the format item, 0 is the start index of the object whose string representation is inserted at that position.
C# int to string Conversion - StringBuilder Method
StringBuilder from the System.Text namespace is a mutable string of characters. A StringBuilder object keeps a buffer to append more characters to the string.
using System;
using System.Text;
public class Demo {
public static void Main() {
// Your code here!
int num = 80;
string numString = new StringBuilder().Append(num).ToString();
System.Console.WriteLine(numString);
}
}
When the argument of StringBuilder is empty, it instantiates a StringBuilder with the value of String.Empty.
Append(num) appends the string representation of num to the StringBuilder.
ToString() methods converts the StringBuilder type to string.
C# int to string Conversion - With + Operator
If a string variable and an int variable are added by the + operator, it will automatically call the int.ToString() method to convert the integer to the string that will be concatenated with the given string variable.
using System;
public class Demo {
public static void Main() {
// Your code here!
int num = 80;
string numString = "" + num;
System.Console.WriteLine(numString);
}
}
Output:
80
string numString = "" + num;
It equivalently converts int to string only if the other variable of the + operator is an empty string - "" or String.Empty.
"" could be either before int or after int. Both are the same in the behavior.
using System;
public class Demo {
public static void Main() {
// Your code here!
int num = 80;
string numString = num + "";
System.Console.WriteLine(numString);
}
}