Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0160 GetType and typeof operator

GetType does the runtime check while the typeof operator does the compile-time check. Both GetType and typeof return System.Type. using System; public class Point { public int X, Y; } class Test { static void Main() { Point p = new Point(); Console.WriteLine(p.GetType().Name); // Point Console.WriteLine(typeof(Point).Name); // Point Console.WriteLine(p.GetType() == typeof(Point)); // True Console.WriteLine(p.X.GetType().Name); // Int32 Console.WriteLine(p.Y.GetType().FullName); // System.Int32 } } The output: Point Point True Int32 System.Int32