Converting an integer to a string in C#

C# - Convert int to string


c# int to string,



C# int to string conversion is actually one of the less painful conversions to do. Converting int to string in C# is easy because everything we need is built right into the library. Keep reading to learn the many ways to convert integers to strings in C#.

Why convert int to string?

Converting an integer to a string is a quite common necessity in modern programming. Consider an example where a user types an input which is captured into an integer variable. If you were asking the user how old they are you may be left with an input of something like 23. Now suppose you need to display this variable back to the user as a string. This is a perfect example of where you need to convert 23 a integer to “23″ a string. Let’s check out a few ways to converting int to string. 

C# int to string conversion

As we mentioned above converting from an int to a string in C# is amazingly simple since everything is already built in to the library! Below is an array of examples (get it) of us turning the example “23” into an integer. Please note that all of these will eventually call the .ToString() method. Calling .ToString() directly is the most efficient way to get from an int to string.

C# int to string examples

int ageNum = 23;
string age = ageNum.ToString();
string age = Convert.ToString(ageNum);
string age = string.Format("{0}", i);
string age = $"{ageNum}";
string age = "" + ageNum;
string age = string.Empty + ageNum;
string age = new StringBuilder().Append(ageNum).ToString();

Source link

Leave a Reply