#660 – The typeof Operator Gets Information About a Type

You can use the typeof operator to get information about a particular type.  The operator returns an instance of the Type class, which you can then query to get info about the type.

typeof is a little different from the GetType method in System.Object.  typeof gets type information for a type, whereas GetType gets type information for an object’s type.

        static void Main()
        {
            Dog kirby = new Dog("Kirby", 13);
            DumpTypeInfoFor(kirby.GetType());

            DumpTypeInfoFor(typeof(Dog));
        }

        private static void DumpTypeInfoFor(Type t)
        {
            Console.WriteLine("Type = {0}", t);
            // etc
        }

Advertisements