Last Updated on May 1, 2024 by Roshan Parihar
To convert int to string in C#, use the integer variable followed by ToString()
method. The conversion of one type of variable to another type is a type of type casting or type conversion.
You can also use the concatenation (+
) operator to add the integer to the string to make it a string. Let’s find out the examples given below.
Convert Int to String Using ToString() in C#
If you want to convert the integer type to string type in C#, you can use the integer variable followed by the ToString()
function. It does not require any argument to pass with the method for the conversion.
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
If the integer variable is ‘myNum’, you have to use the method as myNum.ToString()
as given in the example. The above example shows that the value 21 is first declared as an integer using int
data type. After that, it is converted to a string using the myNum.ToString()
method.
Using String Concatenation (+) to Change Int to String in C#
You can use the concatenation (+
) operator to add the integer variable to the string. The addition of a variable converts it to the string 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 concatenated to the string variable and converts the integer 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. Just 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.
You May Also Like to Read