C# int to string conversion
last modified January 22, 2024
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# 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 article we build string messages that contain an integer.
C# int to string with Int32.ToString
The Int32.ToString
method converts the numeric value
to its equivalent string representation. The int.ToString
is an
alias for the Int32.ToString
.
int val = 4; string msg = "There are " + val.ToString() + " hawks"; Console.WriteLine(msg);
The example uses int.ToString
to do int to string conversion.
$ dotnet run There are 4 hawks
C# int to string with string concatenation
When we use the +
operator on int and string parameters,
the C# compiler internally performs type conversion.
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.
using System.Text; 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#.
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.
Source
In this article we have shown how to perform int to string conversions in C#.
Author
List all C# tutorials.