#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

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: