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 ...
随机推荐
- php获取指定日期所在星期的开始时间与结束时间
function getWeekRange($date){ $ret=array(); $timestamp=strtotime($date); $w=strftime('%u',$ ...
- 分布式文件系统 - FastDFS
分布式文件系统 - FastDFS 别问我在哪里 也许我早已不是我自己,别问我在哪里,我一直在这里. 突然不知道说些什么了... 初识 FastDFS 记得那是我刚毕业后进入的第一家公司,一个技术小白 ...
- Struts2结合sitemesh3制作网站母版页面
上一篇文章介绍了sitemesh3的使用,这篇文章来介绍如何结合struts2来配置和使用sitemesh,具体的如何使用sitemesh3我就不讲解了,这个你们可以看看我的上一篇博客. 首先你要添加 ...
- WPF 多线程处理(1)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 废话不多说,先上图: 多线程处理数据后在th ...
- maven学习心得整理
maven的学习心得 已经接触了maven项目有一段时间了,开始时仅仅会使用,在使用中发现了它的强大和方便,于是决心研究一下: 首先,普及一下maven参数: -D:传入属性参数 -P:使用POM中指 ...
- C++中的mutable关键字
mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词. 在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量,将永远 ...
- PHP 打印函数之 print print_r
print 说明 int print ( string $arg ) 输出 arg print 实际上不是一个函数(它是一个语言结构),因此你可以不必使用圆括号来括起它的参数列表 参数 arg:输入数 ...
- recursion lead to out of memory
There are two storage areas involved: the stack and the heap. The stack is where the current state o ...
- Mutex vs Semaphore
What are the differences between Mutex vs Semaphore? When to use mutex and when to use semaphore? Co ...
- Javascript表格中搜索
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...