Write a program that demonstrates the use of primitive data types in C#. The program should also support the type conversion of Integer to String and String to Integer.

Answer:

using System;

namespace DataTypesAndTypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            int integerVar = 42;
            double doubleVar = 3.14;
            char charVar = 'A';
            bool boolVar = true;
            float floatVar = 5.5f;
            long longVar = 1234567890L;
            short shortVar = 32000;

            Console.WriteLine("Integer: " + integerVar);
            Console.WriteLine("Double: " + doubleVar);
            Console.WriteLine("Character: " + charVar);
            Console.WriteLine("Boolean: " + boolVar);
            Console.WriteLine("Float: " + floatVar);
            Console.WriteLine("Long: " + longVar);
            Console.WriteLine("Short: " + shortVar);

            string integerToString = integerVar.ToString();
            Console.WriteLine("\nConverted Integer to String: " + integerToString);

            string str = "100";
            int stringToInt = Convert.ToInt32(str);
            Console.WriteLine("\nConverted String to Integer: " + stringToInt);

            Console.ReadLine();
        }
    }
}
Scroll to Top