码迷,mamicode.com
首页 > 其他好文 > 详细

typeof与GetType区别

时间:2014-12-17 18:10:53      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:style   sp   for   strong   on   bs   ef   tt   line   

typeof: The typeof operator is used to obtain the System.Type object for a type.

 

运算符,获得某一类型的 System.Type 对象。

Type t = typeof(int);

GetType: Gets the Type of the current instance.

方法,获取当前实例的类型

 int i = 10;

Console.WriteLine(i.GetType());

区别:   Typeof()是运算符而GetType是方法

    • GetType()是基类System.Object的方法,因此只有建立一个实例之后才能够被调用(初始化以后)
    • Typeof()的参数只能是int,string,String,自定义类型,且不能是实例

       

    • GetType() 和typeof()都返回System.Type的引用。

typeof与GetType区别

标签:style   sp   for   strong   on   bs   ef   tt   line   

原文地址:http://www.cnblogs.com/chengjun/p/4169907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!