Last Updated on May 26, 2022 by Roshan Parihar
In this tutorial, learn how to convert int to string in C#. The short answer is to use the C# ToString()
method to change an integer to a string type. The conversion of one type of variable to another type is a type of type casting or type conversion.
You can also convert the integer type to a string type using the concatenation (+
) operator in C#. Concatenation is the way of adding the variables that are also useful for conversion to string type. However, some other methods also available in C# for conversion.
So, let’s start converting the int to string with the examples given here.
How to Convert Int to String Using ToString() in C#
To convert the integer type to string type, you have to use the integer variable followed by the ToString()
method in C#. It does not require any argument to pass with the method for the conversion.
If the integer variable is ‘myNum’, you have to use the method as myNum.ToString()
as given in the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace Demo { class Program { static void Main(string[] args) { //Declare integer variable int myNum = 21; //Convert integer to string type string myStr = myNum.ToString(); //Value after conversion using ToString Console.WriteLine("The string value of "+myNum+" is: "+myStr ); } } } |
Output
The above showing that the value 21 is first declared as an integer using int
with variable myNum
. After that, it is converted to string using the myNum.ToString()
method. The output results in the value 21 and it is an int to string converted value.
Using String Concatenation (+) to Change Int to String in C#
If you want to convert int
to string
, you can use the concatenation of the variable in C#. The concatenation is the way of using the plus (+
) operator to add the integer variable with the string. The addition of a variable converts it to the required type as given in the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; namespace Demo { class Program { static void Main(string[] args) { //Declare integer variable int myNum = 21; //Value after conversion string myStr = "The value " + myNum+" becomes string using concatenation."; Console.WriteLine("The string value of "+myNum+" is: "+myStr ); } } } |
Output
In the above example, the variable myNum
is declared as an integer using the int
data type. After that, the string variable myStr
is declared using the string
data type.
The integer variable myNum
is added with the string variable during the declaration using concatenation. This addition of the integer variable with a string variable converts it into a string data type.
C# Built-in Method Convert.ToString() for Conversion
In addition to the above methods, you can also use the C# built-in method Convert.ToString()
to convert int to string. You also have to pass the integer variable as the argument of the built-in method. This converts the variable to the required data in C#.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace Demo { class Program { static void Main(string[] args) { //Declare integer variable int myNum = 21; //Convert integer to string type string myStr = Convert.ToString(myNum); //Value after conversion Console.WriteLine("The string value of "+myNum+" is: "+myStr ); } } } |
Output
The above example shows that it first declared the integer type variable using the int
data type. After that, it converts the integer type to the string type with the above built-in method.