Different ways for Integer to String Conversions in C#

CsharpProgrammingServer Side Programming

The easiest way to convert an integer to a string in C# is using the ToString() method.

Let us see an example −

int a = 100;
string str = a.ToString();

Another way is to use Convert.ToString();

b = 50;
string str2 = Convert.ToString(b);
Console.WriteLine(str2);

The following is the example showing the different ways to convert integer to string.

Example

 Live Demo

using System;
class Program {
   static void Main() {
      int a, b, c;

      a = 10;
      string str = a.ToString();
      Console.WriteLine(str);

      b = 50;
      string str2 = Convert.ToString(b);
      Console.WriteLine(str2);

      c = 100;
      string str3 = string.Format("{0}", c);
      Console.WriteLine(str3);
   }
}

Output

10
50
100
raja
Published on 16-Aug-2018 09:32:39
Advertisements