Check Datatype in C# | Code By Topic

Check Datatype in C#

By
We already know about the datatype in C#. Some very commonly used data type is string, int, bool, short, long. In this topic we will discuss about how to know what datatype used in assigned variable. In C# we know that each and every class inherited with Object class by default. In this Object Class Virtual method and  Type is there and one of Type is GetType(). We will discuss later about Type in C#.
So with GetType() we are able to find out the type of datatype like is it System.String, System.Int32 and so on. Please find below the code as well as their output. You can also just copy and paste code and check with different way in your Visual Studio IDE.




using System;

namespace CheckDataType
{
    class Program
    {
        static void Main(string[] args)
        {
            char charType = 'A';
            string stringType = "Code By Topic";
            Int16 shortType = 1987;
            Int32 intType = 2010;
            Int64 longType = 2018;
            Boolean boolType = true;
            Console.WriteLine(charType.GetType());
            Console.WriteLine(stringType.GetType());
            Console.WriteLine(shortType.GetType());
            Console.WriteLine(intType.GetType());
            Console.WriteLine(longType.GetType());
            Console.WriteLine(boolType.GetType()); 
     Console.ReadLine();     
 }
    }
}

0 comments:

Post a Comment