Сţµç×ÓÊé > ÆäËûµç×ÓÊé > VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ) >

µÚ24ÕÂ

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ24ÕÂ

С˵£º VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ) ×ÖÊý£º ÿҳ3500×Ö

°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡




is¡¡and¡¡how¡¡you¡¡can¡¡figure¡¡out¡¡what¡¡a¡¡string¡¡can¡¡do¡£¡¡¡¡



Investigating¡¡the¡¡String¡¡Type¡¡



String¡¡is¡¡a¡¡class¡¡type£»¡¡and¡¡thus¡¡a¡¡reference¡¡type¡£¡¡¡¡String¡¡is¡¡a¡¡type¡¡that¡¡has¡¡methods¡¡and¡¡proper

ties¡£¡¡Value¡¡types¡¡like¡¡Double¡¡and¡¡Integer¡¡have¡¡methods¡¡and¡¡properties£»¡¡but¡¡String¡¡is¡¡the¡¡first¡¡¡¡

type¡¡you¡¡are¡¡encountering¡¡that¡¡is¡¡a¡¡class¡¡in¡¡its¡¡own¡¡right¡¡£¨Double¡¡and¡¡Integer¡¡are¡¡not¡¡actually¡¡¡¡

classes£©¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡To¡¡investigate¡¡a¡¡type£»¡¡you¡¡can¡¡read¡¡the¡¡documentation£»¡¡or¡¡you¡¡can¡¡use¡¡IntelliSense¡£¡¡Reading¡¡¡¡

the¡¡documentation¡¡is¡¡not¡¡a¡¡bad¡¡idea£»¡¡but¡¡it¡¡is¡¡the¡¡slowest¡¡and¡¡most¡¡cumbersome¡£¡¡IntelliSense¡¡¡¡

is¡¡an¡¡¡¡IDE¡¡approach£»¡¡in¡¡which¡¡a¡¡defined¡¡type¡¯s¡¡methods¡¡and¡¡properties¡¡are¡¡presented¡¡to¡¡you¡¡in¡¡¡¡

an¡¡easy¡­to¡­prehend¡¡manner¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡time¡¡you¡¡use¡¡IntelliSense£»¡¡it¡¡can¡¡be¡¡nerve¡­racking£»¡¡because¡¡it¡¡seems¡¡like¡¡the¡¡IDE¡¡¡¡

has¡¡a¡¡life¡¡of¡¡its¡¡own¡£¡¡There¡¡is¡¡a¡¡convention¡¡on¡¡how¡¡to¡¡use¡¡IntelliSense£»¡¡as¡¡illustrated¡¡in¡¡Figure¡¡3¡­2¡£¡¡¡¡

I¡¡suggest¡¡that¡¡you¡¡take¡¡a¡¡few¡¡moments¡¡to¡¡experiment¡¡with¡¡it¡£¡¡I¡¡also¡¡remend¡¡that¡¡you¡¡keep¡¡¡¡

IntelliSense¡¡active¡¡in¡¡Visual¡¡Basic¡¡Express¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡78¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

56¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡P¡¡U¡¡L¡¡AT¡¡IO¡¡N¡¡S¡¡¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡3¡­2¡£¡¡¡¡Using¡¡IntelliSense¡¡on¡¡a¡¡String¡¡typed¡¡variable¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡IntelliSense¡¡works¡¡on¡¡only¡¡identified¡¡variables¡¡that¡¡have¡¡types¡£¡¡In¡¡Figure¡¡3¡­2£»¡¡IntelliSense¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡worked¡¡because¡¡the¡¡IDE¡¡parsed¡¡the¡¡code¡¡and¡¡had¡¡the¡¡ability¡¡to¡¡read¡¡the¡¡metadata¡¡associated¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡with¡¡the¡¡type¡£¡¡Metadata¡¡is¡¡data¡¡that¡¡describes¡¡your¡¡source¡¡code¡£¡¡Whenever¡¡you¡¡define¡¡a¡¡class£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡methods¡¡and¡¡properties¡¡are¡¡associated¡¡with¡¡it¡£¡¡The¡¡method¡¡and¡¡property¡¡descriptions¡¡are¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡pieces¡¡of¡¡metadata¡¡that¡¡is¡¡displayed¡¡by¡¡IntelliSense¡£¡¡One¡¡of¡¡the¡¡strengths¡¡of¡¡¡¡is¡¡that¡¡all¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡types¡¡have¡¡metadata¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Basis¡¡of¡¡All¡¡Types£º¡¡Object¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡By¡¡default£»¡¡everything¡¡in¡¡¡¡is¡¡an¡¡object¡¡with¡¡a¡¡few¡¡basic¡¡properties¡¡and¡¡methods¡£¡¡Four¡¡basic¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡methods¡¡are¡¡associated¡¡with¡¡every¡¡object£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Equals£¨£©£º¡¡Verifies¡¡the¡¡equality¡¡of¡¡two¡¡objects¡¡£¨see¡¡Figure¡¡3¡­3£©¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡GetHashCode£¨£©£º¡¡Retrieves¡¡a¡¡unique¡¡number¡¡describing¡¡the¡¡object¡¡£¨see¡¡Figure¡¡3¡­4£©¡£¡¡Two¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡objects¡¡with¡¡the¡¡same¡¡contents¡¡will¡¡return¡¡the¡¡same¡¡hash¡¡code¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡GetType£¨£©£º¡¡Retrieves¡¡the¡¡metadata¡¡associated¡¡with¡¡an¡¡object¡¡£¨see¡¡Figure¡¡3¡­5£©¡£¡¡Allows¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡program¡¡to¡¡play¡¡¡°Are¡¡you¡¡a¡¡¡£¡¡¡£¡¡¡£¡¡£¿¡±¡¡to¡¡dynamically¡¡determine¡¡which¡¡methods¡¡and¡¡proper

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ties¡¡a¡¡type¡¡has¡£¡¡It¡¡is¡¡used¡¡by¡¡IntelliSense¡¡to¡¡display¡¡a¡¡list¡¡box¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡ToString£¨£©£º¡¡Converts¡¡the¡¡contents¡¡of¡¡the¡¡type¡¡into¡¡a¡¡string¡¡£¨see¡¡Figure¡¡3¡­6£©¡£¡¡Note¡¡that¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡default¡¡CLR¡¡ToString£¨£©¡¡implementations¡¡work¡¡only¡¡for¡¡value¡¡types¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡79¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡PU¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡S¡¡¡¡57¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Calling¡¡Equals£¨£©¡¡asks¡¡if¡¡variable¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡has¡¡a¡¡value¡¡of¡¡2¡¡

¡¡Dim¡¡a¡¡As¡¡Integer¡¡=¡¡2¡¡

¡¡If¡¡a¡£Equals£¨2£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨2¡¡equals¡¡2£»¡¡as¡¡it¡¡should¡¡be¡¨£©¡¡

¡¡End¡¡If¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡code¡¡will¡¡always¡¡be¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡called¡¡because¡¡2¡¡equals¡¡2¡¡



Figure¡¡3¡­3¡£¡¡Equals£¨£©¡¡is¡¡used¡¡to¡¡verify¡¡the¡¡equality¡¡of¡¡two¡¡objects¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Don¡¯t¡¡think¡¡of¡¡a£»¡¡b£»¡¡and¡¡c¡¡as¡¡values¡¡associated¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡with¡¡a¡¡variable¡£¡¡Think¡¡of¡¡a£»¡¡b£»¡¡and¡¡c¡¡as¡¡having¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡contents¡¡associated¡¡with¡¡the¡¡variables¡£¡¡The¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡contents¡¡of¡¡a¡¡and¡¡b¡¡match£»¡¡the¡¡contents¡¡of¡¡c¡¡do¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡not¡¡match¡¡the¡¡contents¡¡of¡¡a¡¡and¡¡b¡£¡¡



Dim¡¡a¡¡As¡¡Integer¡¡=¡¡2¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡contents¡¡of¡¡a¡¡and¡¡b¡¡are¡¡

Dim¡¡b¡¡As¡¡Integer¡¡=¡¡2¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡identical£»¡¡thus£»¡¡GetHashCode£¨£©¡¡

Dim¡¡c¡¡As¡¡Integer¡¡=¡¡3¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡returns¡¡the¡¡same¡¡number¡¡



If¡¡a¡£GetHashCode£¨£©¡¡=¡¡b¡£GetHashCode£¨£©¡¡Then¡¡

¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨2¡¡equals¡¡2£»¡¡as¡¡it¡¡should¡¡be¡¨£©¡¡

End¡¡If¡¡



If¡¡c¡£GetHashCode£¨£©¡¡¡¡b¡£GetHashCode£¨£©¡¡Then¡¡

¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨3¡¡does¡¡not¡¡equal¡¡2£»¡¡as¡¡it¡¡should¡¡be¡¨£©¡¡

End¡¡If¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡contents¡¡of¡¡c¡¡and¡¡b¡¡are¡¡not¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡equal£»¡¡so¡¡GetHashCode£¨£©¡¡will¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡return¡¡two¡¡different¡¡numbers¡¡



Figure¡¡3¡­4¡£¡¡GetHashCode£¨£©¡¡retrieves¡¡a¡¡number¡¡describing¡¡the¡¡object¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡four¡¡basic¡¡methods¡¡can¡¡be¡¡called¡¡on¡¡every¡¡variable¡¡that¡¡you¡¡declare¡£¡¡You¡¡will¡¡use¡¡¡¡

ToString£¨£©¡¡when¡¡debugging¡¡or¡¡inspecting¡¡the¡¡state¡¡of¡¡an¡¡object¡¡instance¡¡during¡¡runtime¡£¡¡¡¡

ToString£¨£©¡¡returns¡¡a¡¡human¡­readable¡¡string¡¡that¡¡contains¡¡the¡¡state¡¡of¡¡the¡¡object¡¡instance¡£¡¡

¡¡¡¡¡¡¡¡¡¡You¡¡might¡¡use¡¡the¡¡GetType£¨£©¡¡method¡¡occasionally£»¡¡but¡¡your¡¡IDE¡¡and¡¡other¡¡tools¡¡will¡¡use¡¡¡¡

it¡¡all¡¡the¡¡time¡£¡¡Using¡¡GetType£¨£©£»¡¡you¡¡have¡¡the¡¡ability¡¡to¡¡figure¡¡out¡¡the¡¡capabilities¡¡of¡¡a¡¡variable¡¡¡¡

during¡¡the¡¡execution¡¡of¡¡a¡¡program¡£¡¡In¡¡technical¡¡terms£»¡¡GetType£¨£©¡¡returns¡¡the¡¡formal¡¡metadata¡¡¡¡

description¡¡of¡¡the¡¡type¡£¡¡

¡¡¡¡¡¡¡¡¡¡From¡¡reading¡¡the¡¡description¡¡of¡¡Equals£¨£©¡¡and¡¡GetHashCode£¨£©£»¡¡you¡¡would¡¡get¡¡the¡¡impression¡¡¡¡

that¡¡these¡¡two¡¡functions¡¡serve¡¡the¡¡same¡¡purpose¡£¡¡However£»¡¡this¡¡is¡¡not¡¡the¡¡case¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡80¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

58¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡P¡¡U¡¡L¡¡AT¡¡IO¡¡N¡¡S¡¡¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¡know¡¡that¡¡a¡¡is¡¡a¡¡value¡¡type¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨as¡¡explained¡¡in¡¡Chapter¡¡1£©¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡However£»¡¡a¡¡program¡¡does¡¡not¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡By¡¡calling¡¡GetType£¨£©¡¡and¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡know¡¡that¡¡it¡¡is¡¡a¡¡value¡¡type¡£¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡then¡¡IsValueType£»¡¡the¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡program¡¡is¡¡dynamically¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡a¡¡As¡¡Integer¡¡=¡¡1¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡asking¡¡if¡¡a¡¡is¡¡a¡¡value¡¡type¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡a¡£GetType£¨£©¡£IsValueType¡¡=¡¡True¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨a¡¡is¡¡a¡¡value¡¡type¡¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡code¡¡will¡¡always¡¡be¡¡called¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡because¡¡a¡¡is¡¡always¡¡a¡¡value¡¡type¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡3¡­5¡£¡¡GetType£¨£©¡¡retrieves¡¡the¡¡metadata¡¡associated¡¡with¡¡an¡¡object¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Variable¡¡a¡¡contains¡¡the¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡3¡¡as¡¡a¡¡binary¡¡value¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Calling¡¡ToString£¨£©¡¡will¡¡convert¡¡the¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡binary¡­based¡¡value¡¡into¡¡a¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡character¡­based¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡a¡¡As¡¡Integer¡¡=¡¡3¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡aAsAString¡¡As¡¡String¡¡=¡¡a¡£ToString£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡aAsAString¡¡=¡¡¡¨3¡¨¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨3¡¡has¡¡been¡¡converted¡¡into¡¡a¡¡string¡¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡=¡¡sign¡¡is¡¡another¡¡way¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡°3¡±¡¡always¡¡equals¡¡¡°3¡±¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡test¡¡if¡¡¡°3¡±¡¡equals¡¡¡°3¡±¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡so¡¡this¡¡code¡¡runs¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡3¡­6¡£¡¡¡¡ToString£¨£©¡¡converts¡¡the¡¡contents¡¡of¡¡the¡¡type¡¡into¡¡a¡¡string¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡say¡¡you¡¯re¡¡moving¡¡and¡¡you¡¯ve¡¡packed¡¡two¡¡boxes¡¡containing¡¡kitchen¡¡items¡£¡¡Both¡¡boxes¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡contain¡¡five¡¡red¡¡dishes£»¡¡three¡¡silver¡¡forks£»¡¡two¡¡copper¡¡knives£»¡¡and¡¡two¡¡wineglasses¡£¡¡If¡¡you¡¡pare¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡boxes£»¡¡both¡¡¡¡Equals£¨£©¡¡and¡¡GetHashCode£¨£©¡¡will¡¡return¡¡equality£»¡¡indicating¡¡the¡¡boxes¡¡contain¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡same¡¡number¡¡and¡¡color¡¡of¡¡items¡£¡¡It¡¡is¡¡important¡¡to¡¡understand¡¡that¡¡even¡¡though¡¡the¡¡two¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡boxes¡¡are¡¡unique¡¡instances¡¡containing¡¡unique¡¡articles£»¡¡their¡¡contents¡¡are¡¡identical¡£¡¡When¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡pare¡¡object¡¡instances¡¡using¡¡Equals£¨£©¡¡or¡¡GetHashCode£¨£©£»¡¡you¡¡are¡¡paring¡¡the¡¡metadata¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡value¡¡attributes£»¡¡not¡¡the¡¡unique¡¡instances¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡imagine¡¡the¡¡situation¡¡where¡¡one¡¡of¡¡the¡¡boxes¡¡has¡¡wineglasses¡¡from¡¡IKEA¡¡and¡¡the¡¡other¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡has¡¡wineglasses¡¡from¡¡Pier¡¡1¡£¡¡If¡¡you¡¡use¡¡the¡¡Equals£¨£©¡¡method¡¡to¡¡pare¡¡the¡¡boxes£»¡¡it¡¡will¡¡return¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡False£»¡¡because¡¡the¡¡box¡¡content¡¡details¡¡ar

·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨2£© ²È£¨2£©

Äã¿ÉÄÜϲ»¶µÄ