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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.