C# Convert Int to String

  1. C# int to string Conversion - Int16.ToString() / Int32.ToString() / Int64.ToString() Method
  2. C# int to string Conversion - Convert.ToString() Method
  3. C# int to string Conversion - String.Format() Method
  4. C# int to string Conversion - StringBuilder Method
  5. C# int to string Conversion - With + Operator

C# has different methods to convert int to string. This article introduces methods like ToString method, Convert.ToString method, string formatting, and StringBuilder method.

C# int to string Conversion - Int16.ToString() / Int32.ToString() / Int64.ToString() Method

ToString() method of 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

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 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 + operator, it will automatically call 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 + 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);
    }
}

Related Article - Csharp String

comments powered by Disqus