4 ways to Convert int to string C#

In this post, we are going to learn how to convert int to string in C# with examples. We will use the convert class, convert.ToString() method, string class format(), StringBuilder class append() method, and convert int number to its string representation with a program example.

4 Ways to Convert int to string C#


  • int.ToString():To convert a 16,32,64 bit signed integer to string.
  • Convert.Tostring(): Convert an integer number.
  • string class format() method: Convert a value of an object to a string as per the format given.
  • StringBuilder().Append(): The StringBuilder represents a mutable string and can concatenate multiple strings.

1. Convert class to convert string to int in C#


int32.ToString() to convert a number integer to equivalent a string numeric value. The int32.ToString() is the alias of int.Tostring().In this example, we have assigned the maximum int 32-bit value to an integer number and converted it into the string using int.Tostring().

using System;  
namespace ProgramExample
{
  public class conversion
    {
        static void Main(string[] args)
        {
         
            int mynum = Int32.MaxValue;
           
           string strNum = mynum.ToString();
           Console.WriteLine("changed type:{0}",strNum. GetType());
            Console.WriteLine(strNum);

            Console.ReadLine();
}

    }
}

Output

changed type:System.String
2147483647

2. Convert int to string C# using string.format()


The string class format() method converts a value of an object to a string as per the format given. In this example, we are converting the number to a string and checking its type after conversion by using the GetType() method.

using System;  
namespace ProgramExample
{
  public class conversion
    {
        static void Main(string[] args)
        {
         
            int mynum = Int32.MaxValue;
           
           string strNum = String.Format("int to string {0}",
                         mynum);
           Console.WriteLine("changed type:{0}",strNum. GetType());
            Console.WriteLine(strNum);

            Console.ReadLine();
}

    }
}

Output

changed type:System.String
int to string 2147483647

3. Convert int to string using StringBuilder


The StringBuilder class of System.Text.StringBuilder class can also be used to convert an integer number to a string with the help of the StringBuilder append() method is used to append value into a string and toString() to convert append int value to a string.

using System; 
using System.Text;
namespace ProgramExample
{
  public class conversion
    {
        static void Main(string[] args)
        {
         
            int mynum = Int32.MaxValue;
           
           string strNum = new StringBuilder().Append(mynum).ToString();
       
           Console.WriteLine("changed type:{0}",strNum. GetType());
            Console.WriteLine(strNum);

            Console.ReadLine();
}

    }
}

Output


4. Convert int to string using Convert.Tostring()


The system namespace convert class has static methods Convert. tostring() converts the given value to a string. Let us understand with the below examples.

using System;  
namespace ProgramExample
{
  public class conversion
    {
        static void Main(string[] args)
        {
         
            int mynum = Int32.MaxValue;
           
           string strNum = Convert.ToString(mynum);
           Console.WriteLine("changed type:{0}",strNum. GetType());
            Console.WriteLine(strNum);

            Console.ReadLine();
}

    }
}

Output

changed type:System.String
2147483647