Friday, April 30, 2021

C# System.Convert class Examples

Convert class is a helper class in C# which is used to convert datatype. It is a static class. So, its methods can be accessed by writing dot after the Convert. Its methods are ToXXX() where XXX can be Int32, Int64, Double etc. For details, press F12 keeping cursor at Convert word in Visual Studio.

using System;

namespace Console_ConvertClass
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter first number.");
            double first = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter second number.");
            double second = Convert.ToDouble(Console.ReadLine());
            Console.Write("The sum of numbers is: ");
            Console.WriteLine((first + second).ToString());
            Console.WriteLine("Enter your date of birth as per sytem date format.");
            DateTime dt = Convert.ToDateTime(Console.ReadLine());
            Console.WriteLine(dt.ToString("D"));
        }
    }
}

OUTPUT:
Enter first number.
25
Enter second number.
41
The sum of numbers is: 66
Enter your date of birth as per sytem date format.
12/15/2010
December 15, 2010

No comments:

Post a Comment

Hot Topics