Ebooks

C# int to string conversion tutorial

C# int to String tutorial shows how to convert integers to strings. There are several ways to perform int to String conversion in C#. We can use string concatenation, string formatting, string building, and use built-in conversion methods.

C# tutorial is a comprehensive tutorial on C# language.

C# int to string conversion

Integer to string conversion is a type conversion or type casting, where an entity of integer data type is changed into string one.

In the examples of this tutorial, we build string messages that contains an integer.

C# int to string with Int32.ToString()

The Int32.ToString() method converts the numeric value to its equivalent string representation.

Program.cs
using System;

namespace ToStringMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            int val = 4;
            string msg = "There are " + val.ToString() + " hawks";

            Console.WriteLine(msg);
        }
    }
}

The example uses Int32.ToString() to do int to string conversion.

$ dotnet run
There are 4 hawks

This is the output.

C# int to string with string concatenation

When we use the + operator on int and string parameters, the C# compiler internally performs type conversion.

Program.cs
using System;

namespace ConcatEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int numOfApples = 16;
            string msg = "There are " + numOfApples + " apples";

            Console.WriteLine(msg);
        }
    }
}

The example uses string concatenation to do int to string conversion.

C# int to string with StringBuilder

StringBuilder represents a mutable string of characters. We can use StringBuilder to construct strings. We can append integers to the builder as well.

Program.cs
using System;
using System.Text;

namespace StringBuilderEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int val = 4;
            var builder = new StringBuilder();

            builder.Append("There are ");
            builder.Append(val).ToString();
            builder.Append(" hawks");

            Console.WriteLine(builder);
        }
    }
}

The code example uses StringBuilder to do int to string conversion.

C# int to string other examples

The following example provides other ways to the int to string conversion in C#.

Program.cs
using System;

namespace IntToStringEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int val = 4;

            string msg = "There are " + Convert.ToString(val) + " hawks";
            string msg2 = string.Format("There are {0} hawks", val);
            string msg3 = $"There are {val} hawks";

            Console.WriteLine(msg);
            Console.WriteLine(msg2);
            Console.WriteLine(msg3);
        }
    }
}

We use Convert.ToString(), string.format(), and string interpolation to do int to string conversions.

In this tutorial, we have shown how to perform int to string conversions in C#.

Read C# tutorial or list all C# tutorials.