How to use Object.GetType() method to gets the type of the current instance in C#

4 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 5;
            long l = 234;

            Console.WriteLine("n type: {0}", n.GetType());
            Console.WriteLine("l type: {0}", l.GetType());
        }
    }
}


/*
run:
   
n type: System.Int32
l type: System.Int64
  
*/

 



answered Apr 25, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 5;
            long l = 234;

            Console.WriteLine("is n and l the same type: {0}", 
                               Object.ReferenceEquals(n.GetType(), l.GetType()));
        }
    }
}


/*
run:
   
is n and l the same type: False
  
*/

 



answered Apr 25, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] o_types = { 87, (long)9835, (byte)3, (sbyte)-8, 3.14, (float)5.87, "string" };

            foreach (var value in o_types)
            {
                Type t = value.GetType();

                if (t.Equals(typeof(byte)))
                    Console.WriteLine("{0, 4}: is 8-bit unsigned byte.", value);
                else if (t.Equals(typeof(sbyte)))
                    Console.WriteLine("{0, 4}: is 8-bit signed byte.", value);
                else if (t.Equals(typeof(int)))
                    Console.WriteLine("{0, 4}: is a 32-bit integer (int).", value);
                else if (t.Equals(typeof(long)))
                    Console.WriteLine("{0, 4}: is a 64-bit integer (long).", value);
                else if (t.Equals(typeof(float)))
                    Console.WriteLine("{0, 4}: is a single-precision floating-point (float).", value);
                else if (t.Equals(typeof(double)))
                    Console.WriteLine("{0, 4}: is a double-precision floating-point (double).", value);
                else if (t.Equals(typeof(string)))
                    Console.WriteLine("'{0, 4}': is Unicode characters (string).", value);
            }
        }
    }
}


/*
run:
   
  87: is a 32-bit integer (int).
9835: is a 64-bit integer (long).
   3: is 8-bit unsigned byte.
  -8: is 8-bit signed byte.
3.14: is a double-precision floating-point (double).
5.87: is a single-precision floating-point (float).
'string': is Unicode characters (string).
  
*/

 



answered Apr 25, 2016 by avibootz
0 votes
using System;

public class BaseClass {}

public class DerivedClass : BaseClass {}

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        public const int N = 20; 

        static void Main(string[] args)
        {
            BaseClass baseC = new BaseClass();
            DerivedClass derived = new DerivedClass();
            object o = derived;
            BaseClass bc = derived;

            Console.WriteLine("baseC type: {0}\n", baseC.GetType());
            Console.WriteLine("derived type: {0}\n", derived.GetType());
            Console.WriteLine("object o = derived type: {0}\n", o.GetType());
            Console.WriteLine("BaseClass bc = derived type: {0}\n", bc.GetType());
        }
    }
}


/*
run:
   
baseC type: BaseClass

derived type: DerivedClass

object o = derived type: DerivedClass

BaseClass bc = derived type: DerivedClass
  
*/

 



answered Apr 27, 2016 by avibootz
...