The difference of typeof and GetType method

来源:互联网 发布:mmd口型数据 编辑:程序博客网 时间:2024/04/20 21:17

 first,we will say the typeof is the type of the class,but the GetType is the class of instance

 

 second,typeof is in the compile time,the GetType is in the runtime

 

 and the GetType is a vitual method,so must care

 

code:

 

   Animal animal = new Animal();

            Dog dog = new Dog();

            Animal ADog = new Dog();

            Response.Write(animal.GetType() + "——animal.GetType() </br> ");
            Response.Write(dog.GetType() + "——dog.GetType() </br> ");
            Response.Write(ADog.GetType() + "——ADog.GetType() </br> ");

            Response.Write(typeof(Animal) + "——typeof(Animal) </br> ");
            Response.Write(typeof(Dog) + "——typeof(Dog) </br> ");

 

result:

 

无标题页KDia.testtypeof.Animal——animal.GetType()
KDia.testtypeof.Dog——dog.GetType()
KDia.testtypeof.Dog——ADog.GetType()
KDia.testtypeof.Animal——typeof(Animal)
KDia.testtypeof.Dog——typeof(Dog)

 

  so the GetType() method is in the runtime,the typeof is in the compile time

 

 

 

原创粉丝点击