C# program to convert various data type to string

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

using System.Threading;

using System.IO;


namespace CSharpProgram

{

    enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }

    class AkbTechie

     {

            static void Main(string[] args)

                {

                     // Integer to String

                    int i = 20;

                    string NumAsString = i.ToString();

                    Console.WriteLine("Integer value to String: " + NumAsString);


                    // Float to String

                    float f = 20.15f;

                    string FloatAsString = f.ToString();

                    Console.WriteLine("Float value to String: " + FloatAsString);


                    // Double to String

                    double d = 8.13;

                    string doubleAsString = d.ToString();

                    Console.WriteLine("Double value to String: " + doubleAsString);


                    // Double to String

                    bool b = true;

                    string boolAsString = b.ToString();

                    Console.WriteLine("Boolean Value to String: " + boolAsString);


                    // DateTime to String

                    DateTime now = DateTime.Now;

                    string nowAsString = now.ToString();

                    Console.WriteLine("DateTime to String: " + nowAsString);


                    // String Concatenation with ToString()

                    int x = 5;

                    int y = 8;

                    string result = "The sum is: " + (x + y).ToString();

                    Console.WriteLine("String Concatenation value To String: " + result);


                    // Enum to String

                    Day today = Day.Monday;

                    string dayAsString = today.ToString();

                    Console.WriteLine("Enum to String: " + dayAsString);

                    Console.ReadKey();

          }

    }

}


Output:-

Integer value to String: 20

Float value to String: 20.15

Double value to String: 8.13

Boolean Value to String: True

DateTime to String: 09-06-2024 07:53:42

String Concatenation value To String: The sum is: 13

Enum to String: Monday 


एक टिप्पणी भेजें

0 टिप्पणियाँ