http://stackoverflow.com/questions/4537945/what-is-the-difference-of-getting-type-by-using-gettype-and-typeof

You can only use typeof() when you know that type at compile time, and you're trying to obtain the corresponding Type object. (Although the type could be a generic type parameter, e.g. typeof(T) within a class with a type parameter T.) There don't need to be any instances of that type available to use typeof. The operand for typeof is always the name of a type or type parameter. It can't be a variable or anything like that.

Now compare that with object.GetType(). That will get the actual type of the object it's called on. This means:

  • You don't need to know the type at compile time (and usually you don't)
  • You do need there to be an instance of the type (as otherwise you have nothing to call GetType on)
  • The actual type doesn't need to be accessible to your code - for example, it could be an internal type in a different assembly

One odd point: GetType will give unexpected answers on nullable value types due to the way that boxing works. A call to GetType will always involve boxing any value type, including a nullable value type, and the boxed value of a nullable value type is either a null reference or a reference to an instance of a non-nullable value type.

typeof和GetType的区别的更多相关文章

  1. C# typeof 与GetType()的区别

    C#中Type类的介绍:https://msdn.microsoft.com/zh-cn/library/system.type(VS.80).aspx C#中任何对象都具有GetType()方法,它 ...

  2. typeof与GetType

    typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符,获得某一类型的 System. ...

  3. typeof与GetType区别及反射的见解

    http://www.cnblogs.com/knowledgesea/archive/2013/03/02/2935920.html http://www.cnblogs.com/Jax/archi ...

  4. 值类型与引用类型(特殊的string) Typeof和GetType() 静态和非静态使用 参数传递 相关知识

    学习大神博客链接: http://www.cnblogs.com/zhili/category/421637.html 一 值类型与引用类型 需要注意的string 是特殊类型的引用类型. 使用方法: ...

  5. call()与apply()区别typeof和instanceof的区别

    摘自 http://www.cnblogs.com/qzsonline/archive/2013/03/05/2944367.html 一.方法的定义 call方法: 语法:call(thisObj, ...

  6. typeof()和instanceof()用法区别

    typeof()和instanceof()用法区别: 两者都是用来判断数据类型的 typeof()是能用来判断是不是属于五大类型Boolean,Number,String,Null,Undefined ...

  7. typeof和instanceof的区别

    typeof和instanceof的区别: typeof typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型.它返回值是一个字符串,该字符串说明运算数的类型.typeof 一般只能 ...

  8. C# typeof() 和 GetType()区别

    1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...

  9. c# typeof 与 GetType 作用与区别

    官方解释: Used to obtain the "System.Type" object for a type. A 'typeof' expression takes the ...

随机推荐

  1. iOS应用的真机调试

    必须条件:99美元的帐号,没有这个就不用再往下看了. 首先,登录到http://developer.apple.com/devcenter/ios/index.action,如果已经购买了iPhone ...

  2. matlab实现插值法sin函数

    插值法实现sin函数: %calculate and print the sine function %input: x %output: sin(x) similar function y = si ...

  3. 使用ASP.NET注册工具aspnet_regiis.exe注册IIS

    该工具的名称为aspnet_regiis.exe,在32位机上,该工具存在于C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727,在64位机中“Framework ...

  4. GitHub error “Failed to get HEAD”

    cd /要提交的文件的文件夹下    比如要提交一个名为  demo的  程序, 那么先进入demo 的文件夹里面  然后  进行以下两步 git init  (有时这个是不必要的,因为xcode 自 ...

  5. SQL Server表分区【转】

    转自:http://www.cnblogs.com/knowledgesea/p/3696912.html SQL Server表分区   什么是表分区 一般情况下,我们建立数据库表时,表数据都存放在 ...

  6. IntelliJ IDEA 15 部署Tomcat及创建一个简单的Web工程

    一.部署Tomcat 二.创建一个简单的Web工程 2.1创建一个新工程 创建一个新工程 设置JDK及选择Web Application (创建的是Web工程) 点击Next,选择工作空间,起个工程名 ...

  7. apache nginx php不显示版本号

    apache 不显示版本号 http.conf 中的 修改为 ServerTokens ProdServerSignature Off 有的版本没有,在最后添加即可 php php.ini 中的 修改 ...

  8. 导致Asp.Net站点重启的10个原因 ,记录重启原因

    Asp.Net站点有时候会莫名其妙的重启,什么原因导致的却不得而知,经过一番折腾后,我总结了导致Asp.Net站点重启的10个原因 1. 回收应用程序池会导致站点重启,记录的原因是: HostingE ...

  9. tomcat 解析(二)-消息处理过程

    接下来我们应该去了解一下 tomcat 是如何处理jsp和servlet请求的. 1.  我们以一个具体的例子,来跟踪TOMCAT, 看看它是如何把Request一层一层地递交给下一个容器, 并最后交 ...

  10. POJ 3125 Printer Queue(队列,水题)

    题意:有多组数据,每组数据给出n,m,n表示需要打印的文件个数,m表示要打印的目标位置(m为0~n-1).    接下来给出n个数,第i个值对应第i-1个位置的优先级大小.    打印规则如下:    ...