惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
IT之家
IT之家
博客园 - 叶小钗
雷峰网
雷峰网
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美

博客园 - {:)

[译]删除IE收藏夹下的“链接”文件夹 Javascript Cheat Sheet(Javascript 速查手冊) CSS Cheat-Sheet(CSS速查手冊) 我们不需要企业偶像,我们需要企业英雄 [轉]jQuery selectors (JQuery选择器) 和JQuery Cheat sheet (JQuery 速查手冊) YKWIM - {:) - 博客园 the status code returned from the server was:500 Don't use GetType() with Page.ClientScript.RegisterClientScriptBlock() DNS 服務器地址被篡改 Outlook2007 連接到Exchange Server 失敗 装了KIS2009之后 10月22日链接篇: ASP.NET, Visual Studio, WPF 和 Silverlight - Scott Guthrie 博客中文版 - 博客堂 The Coding Standard Communication with the underlying transaction manager has failed 程序员八荣八耻 SCORM -LMS-SCO Data课件与平台交互机制圖解 解决安装SP3后,Window Media Player 播放器不能播放的问题 解決VS無法拖拽控件到頁面的問題 Some JavaScript Source Code About WebInput Use Refresh() function right in WebGrid with javascript
Discover the difference among Types: is operator,typeof k...
{:) · 2008-11-14 · via 博客园 - {:)
  • is operator - shows if object x is of type y. If this condition is true, then you can cast the instance to the given type. This accounts for inheritance. For example, all types (such as System.Int32) inherit from System.Object.  And 'is' Check if we can cast  that type  safety to  this type. Also We can use the '' as ''
  • keyword when we want to see if we can cast form one type to another,if  the cast is not valid then the ''as'' operator will return null.

  • GetType() method - a method on System.Object (and therefore available to all types) that returns the Type of the object. The Type class has a "FullName" property which returns a string of the name. Notice that you can tell the name of immediate type here, but it doesn't inheritance. For example, for an object on type "System.Int32"  the "is" keyword accounts for the current type (int) and the base type (object). GetType requires an instance and returns a type.
  • typeof keyword - Takes a Class an returns a Type (almost the inverse of typeof).
  • "instance.GetType()==typeof(Class) " is not always equal  "instance is Class".there will one  problem when we use inheritance.

    I.E:

        BasePage inherits form Page,and BasePage inherits also from object,so if we

        test for(new BasePage()).GetType()==typeof(object) it'll return false because

         the types are different,but when we test using the is operator it's true.

         ((new BasePage()) is object) is true because (new BasePage()) is an object of   type BasePage, and also a Page and an object. The types might be different , but is operator checks whethere we can cast safely to this type.

    Notice :

    this same pattern can be applied to both value types (like a System.Int32) and reference types (like an XmlDocument):

    typeof() is an operator and GetType() is a method defined in System.Object an both of them returns and object of System.Type.

    System.Type.GetType is more than 7 times slower (on my dual core machine) than typeof.

    There are times when GetType is useful, but if the Type is well known to you then use typeof.

    use:

    Type aType = typeof(DateTime);

    instead of:

    System.Type.GetType("System.DateTime");

    By using the is operator, GetType method, and typeof keyword, we can effectively manage type information and solve several problems in various ways.