How to convert int to string without using Convert.ToString() Method in C#.NET?

 

Convert.ToString() method is used to convert integer to string.

There are many other way to convert integer to sting  as mention below.

 

ToString(): ToString() method is present in System namespace. It is used to convert integer to string.

//integer x
int x = 100;

//convert int to string using tostring() method
string s1 = x.ToString();

//Output 100
Console.WriteLine(s1);

String.Format(): It is used to convert object to string based on format specified.

Int value x is converted to string using String.Format function in below example.

 

//integer x
int x = 100;

//convert int to string using string.format
string s2 = string.Format("{0}",x);

//Output 100
Console.WriteLine(s2);

If you want to know more information about String.Format then refer below link.

https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.7.2#Starting

 

StringBuilder:

String is immutable, means string cannot be changed hence stringbuilder is used for string modification.

 

Here I created object of StringBuilder and appended the int value x then converted builder object to string.

//integer x
int x = 100;

//convert int to string using string builder
StringBuilder builder = new StringBuilder();
string s3 = builder.Append(x).ToString();

//Output 100
Console.WriteLine(s3);

 

Append String:

If we append string value to int value then int value will be converted to string.

//integer x
int x = 100;


//convert to string by adding to another string
string s4 = "" + x;

//Output 100
Console.WriteLine(s4);

 

Please provide your feedback and comments.

Happy coding Smile

Be the first to comment

Leave a Reply

Your email address will not be published.


*