CSharp - Write program to Convert number to String


You will convert from a number to text.


To convert a number into its textual representation, use the ToString method.

To convert anything into string, you can always use the ToString method.

A value of type int cannot be directly assigned to a variable of type string.

You have to convert it to text form first.


using System;

class Program//from w w  w  .j  av a  2 s .  c o  m
    static void Main(string[] args)
        // Some number 
        int number = 1234;

        // Conversion to text 
        //string numberAsText = number; // DOES NOT WORK! 
        string numberAsText = number.ToString();

        // Output 
        Console.WriteLine("Output of number: " + number);
        Console.WriteLine("Output of text: " + numberAsText);



Console.WriteLine method converts everything it gets into text.

It does the converting using the ToString conversion behind the scenes.

If you join some text with a number using the plus sign, the number gets automatically converted to text.