#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
        }

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #660 – The typeof Operator Gets Information About a Type

  1. Pingback: #1,144 – Getting Type Information about a Generic Type | 2,000 Things You Should Know About C#

Leave a comment