<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>守望轩 &#187; AS</title> <atom:link href="http://www.watch-life.net/tag/as/feed" rel="self" type="application/rss+xml" /><link>http://www.watch-life.net</link> <description>关注IT,审视自我,守望生活, 宁静雅致的小轩馆.</description> <lastBuildDate>Wed, 08 Sep 2010 16:03:38 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>c#细节(三)-AS和IS</title><link>http://www.watch-life.net/csharp/csharp-detail-modifiers-as-is.html</link> <comments>http://www.watch-life.net/csharp/csharp-detail-modifiers-as-is.html#comments</comments> <pubDate>Mon, 07 Jul 2008 09:26:54 +0000</pubDate> <dc:creator>xjb</dc:creator> <category><![CDATA[c#]]></category> <category><![CDATA[AS]]></category> <category><![CDATA[IS]]></category> <category><![CDATA[操作符]]></category> <category><![CDATA[细节]]></category><guid
isPermaLink="false">http://www.watch-life.net/?p=150</guid> <description><![CDATA[《c#的细节》是我当初学习c#时候，做的一些学习笔记的整理，现在回头看来这些都是非常浅显的知识，是c#非常细节的地方，同时也是不能忽略的地方，”细节决定成败“，也许有些严重。我希望把这些整理出来，可以给初学者一个参考,也给自己一个温故知新的机会。高手就不必花时间来看了。;-) &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- 在c#中类型的转换分两种：显式和隐式，基本的规则如下： 1、基类对象转化为子类对象，必须显式转换，规则：(类型名) 对象。 2、值类型和引用类型的转换采用装箱(boxing)或拆箱(unboxing). 3、子类转化为基类对象。 4、基本类型互相之间转化可以用Covent类来实现。 5、字符串类型转换为对应的基本类型用Parse方法，除了String类型外其他的类型都可以用Parse方法。 6、用GetType可以取得对象的精确类型。 7、子类转化为基类，采用隐式转换。 下面来说说AS和IS操作符。 AS：用于在兼容的引用类型之间执行转换。 IS：检查对象是否与给定类型兼容 从概念来分析： 1、AS在转换的同事兼判断兼容性，如果无法进行转换，则 as 返回 null（没有产生新的对象）而不是引发异常。有了AS我想以后就不要再用try-catch来做类型转换的判断了。因此as转换成功要判断是否为null。 2、AS是引用类型类型的转换或者装箱转换，不能用与值类型的转换。如果是值类型只能结合is来强制转换，代码如下： object objTest = 11; if( objTest is int ) { &#160;&#160;&#160; int nValue = (int )objTest; } 3、IS只是做类型兼容判断，并不执行真正的类型转换。返回true或false，不会返回null，对象为null也会返回false。 4、AS模式的效率要比IS模式的高，因为借助IS进行类型转换的化，需要执行两次类型兼容检查。而AS只需要做一次类型兼容，一次null检查，null检查要比类型兼容检查快。 本文地址：http://www.watch-life.net/csharp/csharp-detail-modifiers-as-is.html 你可能也会喜欢以下文章： c#细节&#8211;Property和Attribute迷局 (1) WebResponse 跨域访问 (1) Sqlite数据库的加密 (0) web developer tips (26):在 App_Code目录下同时放c#和VB.NET文件 (0) 温故知新的经典&#8211;书评《Programming [...]]]></description> <content:encoded><![CDATA[<p><span
style="color: #0000ff;">《c#的细节》是我当初学习c#时候，做的一些学习笔记的整理，现在回头看来这些都是非常浅显的知识，是c#非常细节的地方，同时也是不能忽略的地方，”细节决定成败“，也许有些严重。我希望把这些整理出来，可以给初学者一个参考,也给自己一个温故知新的机会。高手就不必花时间来看了。;-)</span></p><p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br
/> 在<a
href="http://www.watch-life.net/category/csharp" target="_blank">c#</a>中类型的转换分两种：显式和隐式，基本的规则如下：<br
/> 1、基类对象转化为子类对象，必须显式转换，规则：(类型名) 对象。<br
/> 2、值类型和引用类型的转换采用装箱(boxing)或拆箱(unboxing).<br
/> 3、子类转化为基类对象。<br
/> 4、基本类型互相之间转化可以用Covent类来实现。<br
/> 5、字符串类型转换为对应的基本类型用Parse方法，除了String类型外其他的类型都可以用Parse方法。<br
/> 6、用GetType可以取得对象的精确类型。<br
/> 7、子类转化为基类，采用隐式转换。</p><p>下面来说说<a
href="http://www.watch-life.net/csharp/csharp-detail-modifiers-as-is.html">AS和IS操作符</a>。<br
/> AS：用于在兼容的引用类型之间执行转换。<br
/> IS：检查对象是否与给定类型兼容</p><p>从概念来分析：<br
/> 1、AS在转换的同事兼判断兼容性，如果无法进行转换，则 as 返回 null（没有产生新的对象）而不是引发异常。有了AS我想以后就不要再用try-catch来做类型转换的判断了。因此as转换成功要判断是否为null。<br
/> 2、AS是引用类型类型的转换或者装箱转换，不能用与值类型的转换。如果是值类型只能结合is来强制转换，代码如下：</p><div
class="code"><span
style="color: #0000ff;">object</span><span
style="color: #000000;"> objTest </span><span
style="color: #000000;">=</span><span
style="color: #000000;"> </span><span
style="color: #800080;">11</span><span
style="color: #000000;">; </span></p><p> <span
style="color: #000000;"><span
style="color: #0000ff;">if</span><span
style="color: #000000;">( objTest </span><span
style="color: #0000ff;">is</span><span
style="color: #000000;"> </span><span
style="color: #0000ff;">int</span><span
style="color: #000000;"> )</span></span></p><p><span
style="color: #000000;"> {</span><br
/> <span
style="color: #000000;"> <span
style="color: #0000ff;">&nbsp;&nbsp;&nbsp; int</span><span
style="color: #000000;"> nValue </span><span
style="color: #000000;">=</span><span
style="color: #000000;"> (</span><span
style="color: #0000ff;">int</span><span
style="color: #000000;"> )objTest;</span></span></p><p> <span
style="color: #000000;"><span
style="color: #000000;"> }<br
/> </span></div><p>3、IS只是做类型兼容判断，并不执行真正的类型转换。返回true或false，不会返回null，对象为null也会返回false。<br
/> 4、AS模式的效率要比IS模式的高，因为借助IS进行类型转换的化，需要执行两次类型兼容检查。而AS只需要做一次类型兼容，一次null检查，null检查要比类型兼容检查快。</p><p>本文地址：<a
href="http://www.watch-life.net/csharp/csharp-detail-modifiers-as-is.html">http://www.watch-life.net/csharp/csharp-detail-modifiers-as-is.html</a></p><p
class="akst_link"></p><h4>你可能也会喜欢以下文章：</h4><ul
class="st-related-posts"><li><a
href="http://www.watch-life.net/csharp/csharp-detail-property-attribute.html" title="c#细节&#8211;Property和Attribute迷局 (2008-07-17)">c#细节&#8211;Property和Attribute迷局</a> (1)</li><li><a
href="http://www.watch-life.net/aspnet/webresponse-cross-site-visit.html" title="WebResponse 跨域访问 (2008-07-07)">WebResponse 跨域访问</a> (1)</li><li><a
href="http://www.watch-life.net/net-tip/sqlite-encrypted.html" title="Sqlite数据库的加密 (2008-07-25)">Sqlite数据库的加密</a> (0)</li><li><a
href="http://www.watch-life.net/visual-studio/have-c-and-vb-net-files-inside-your-app-code-directory.html" title="web developer tips (26):在 App_Code目录下同时放c#和VB.NET文件 (2009-07-19)">web developer tips (26):在 App_Code目录下同时放c#和VB.NET文件</a> (0)</li><li><a
href="http://www.watch-life.net/technology-book-review/programming-csharp-bookreview.html" title="温故知新的经典&#8211;书评《Programming C# 》中文版第4版 (2007-10-30)">温故知新的经典&#8211;书评《Programming C# 》中文版第4版</a> (1)</li><li><a
href="http://www.watch-life.net/csharp/csharp-detail-question-marks.html" title="c#的细节(一)-问号的细节 (2008-07-03)">c#的细节(一)-问号的细节</a> (0)</li><li><a
href="http://www.watch-life.net/life-thinking/work-and-life.html" title="求职与做人 (2008-10-30)">求职与做人</a> (6)</li><li><a
href="http://www.watch-life.net/csharp/csharp-detail-modifiers.html" title="c#的细节(二)-修饰符 (2008-07-06)">c#的细节(二)-修饰符</a> (0)</li></ul>转载请注明作者，出处，以及原始超链接。<br
/>评论数：0 | <b><a
href='http://www.watch-life.net/csharp/csharp-detail-modifiers-as-is.html#comment'>评论这篇文章</a></b>|关注作者最近动态,欢迎 follow me <a
href='https://twitter.com/xjb' target='_blank'>xjb@twitter.com</a>|联络作者：iamxjb@gmail.com<br/>© 2010 守望轩 <a
href='http://www.watch-life.net/csharp/csharp-detail-modifiers-as-is.html'>www.watch-life.net</a>]]></content:encoded> <wfw:commentRss>http://www.watch-life.net/csharp/csharp-detail-modifiers-as-is.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Served from: www.watch-life.net @ 2010-09-09 11:20:31 by W3 Total Cache -->