typeof和GetType的区别
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的区别的更多相关文章
- C# typeof 与GetType()的区别
C#中Type类的介绍:https://msdn.microsoft.com/zh-cn/library/system.type(VS.80).aspx C#中任何对象都具有GetType()方法,它 ...
- typeof与GetType
typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符,获得某一类型的 System. ...
- typeof与GetType区别及反射的见解
http://www.cnblogs.com/knowledgesea/archive/2013/03/02/2935920.html http://www.cnblogs.com/Jax/archi ...
- 值类型与引用类型(特殊的string) Typeof和GetType() 静态和非静态使用 参数传递 相关知识
学习大神博客链接: http://www.cnblogs.com/zhili/category/421637.html 一 值类型与引用类型 需要注意的string 是特殊类型的引用类型. 使用方法: ...
- call()与apply()区别typeof和instanceof的区别
摘自 http://www.cnblogs.com/qzsonline/archive/2013/03/05/2944367.html 一.方法的定义 call方法: 语法:call(thisObj, ...
- typeof()和instanceof()用法区别
typeof()和instanceof()用法区别: 两者都是用来判断数据类型的 typeof()是能用来判断是不是属于五大类型Boolean,Number,String,Null,Undefined ...
- typeof和instanceof的区别
typeof和instanceof的区别: typeof typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型.它返回值是一个字符串,该字符串说明运算数的类型.typeof 一般只能 ...
- C# typeof() 和 GetType()区别
1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...
- c# typeof 与 GetType 作用与区别
官方解释: Used to obtain the "System.Type" object for a type. A 'typeof' expression takes the ...
随机推荐
- ios6 处理内存警告
iPhone下每个app可用的内存是被限制的,如果一个app使用的内存超过20M,则系统会向该app发送Memory Warning消息.收到此消息后,app必须正确处理,否则可能出错或者出现内存泄露 ...
- php集成开发环境IDE
ZendStudio EclipsePHP PhpStorm NetBeans
- ios检查版本更新
场景 在我们使用应用时,一打开应用,如果此应用有新的版本,常常能在应用中给出提示,是否要更新此应用.所以,我们就来看看,版本更新是如何实现的. 应用 苹果给了我们一个接口,能根据应用i ...
- 微信消息处理JAXP-dom解析
package cn.lihainan.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import ...
- (ASP.NET)C#连接Oracle数据库示例(中文乱码问题解决)
接手了一个遗留的ASP.NET系统,数据库用的是Oracle,以前没搞过.NET和Oracle数据库,数据库搞了半天才解决乱码问题,在此做个笔记备忘. 1.下载安装ODAC 1)请去Oracle官网下 ...
- linux使用:vi编辑器
初学linux,目前是概念多于操作,所以记录下一些操作: 编辑某个文件():vi 文件名 编辑后保存退出::wq 编辑后不保存退出: :q! 参数:-R 只读模式 -x 文件加密(vim命令下使用) ...
- poj 2362
回溯加剪枝 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...
- (一)、http原理
谣言粉碎机前些日子发布的<用公共WiFi上网会危害银行账户安全吗?>,文中介绍了在使用HTTPS进行网络加密传输的一些情况,从回复来看,争议还是有的.随着网络越来越普及,应用越来越广泛,一 ...
- POJ 1258 Agri-Net(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...
- Javascript的9张思维导图学习
思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又极其有效,是一种革命性的思维工具.思维导图运用图文并重的技巧,把各级主题的关系用相互隶属与相关的层级图表现出来 ...