ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ18²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡are¡¡not¡¡identical¡£¡¡The¡¡difference¡¡lies¡¡in¡¡how¡¡the¡¡glasses¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡81¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡¡¡59¡¡
are¡¡described¡£¡¡In¡¡contrast£»¡¡calling¡¡GetHashCode£¨£©¡¡will¡¡indicate¡¡that¡¡the¡¡contents¡¡of¡¡the¡¡boxes¡¡are¡¡¡¡
identical¡£¡¡This¡¡is¡¡because¡¡GetHashCode£¨£©¡¡is¡¡doing¡¡a¡¡quick¡¡contents¡¡identification¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡The¡¡difference¡¡between¡¡the¡¡¡¡Equals£¨£©¡¡and¡¡GetHashCode£¨£©¡¡methods¡¡is¡¡perspective¡£¡¡In¡¡the¡¡¡¡
example£»¡¡from¡¡the¡¡perspective¡¡of¡¡the¡¡moving¡¡pany£»¡¡the¡¡boxes¡¡are¡¡identical¡¡because¡¡the¡¡¡¡
moving¡¡pany¡¡does¡¡not¡¡care¡¡whether¡¡the¡¡wineglasses¡¡are¡¡from¡¡IKEA¡¡or¡¡Pier¡¡1£»¡¡it¡¡will¡¡not¡¡¡¡
distinguish¡¡wineglasses¡¡from¡¡different¡¡panies¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡The¡¡fact¡¡that¡¡GetHashCode£¨£©¡¡can¡¡return¡¡identical¡¡numbers¡¡for¡¡what¡¡would¡¡seem¡¡dissimilar¡¡¡¡
object¡¡contents¡¡can¡¡confuse¡¡developers¡£¡¡The¡¡way¡¡to¡¡understand¡¡GetHashCode£¨£©¡¡is¡¡that£»¡¡rather¡¡¡¡
than¡¡being¡¡useful¡¡to¡¡verify¡¡equality£»¡¡it¡¡helps¡¡you¡¡verify¡¡¡¡inequality¡£¡¡If¡¡two¡¡objects¡¡return¡¡dissim
ilar¡¡hash¡¡code¡¡values£»¡¡then¡¡you¡¡know¡¡the¡¡contents¡¡are¡¡not¡¡identical¡£¡¡The¡¡intent¡¡of¡¡a¡¡hash¡¡code¡¡¡¡
is¡¡to¡¡generate¡¡a¡¡quick¡¡fingerprint¡¡of¡¡the¡¡contents¡¡of¡¡an¡¡object¡£¡¡It¡¡is¡¡not¡¡pletely¡¡reliable£»¡¡but¡¡¡¡
works¡¡for¡¡the¡¡most¡¡part¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡WHEN¡¡INTELLISENSE¡¡IS¡¡NOT¡¡ENOUGH¡¡¡¡
¡¡¡¡¡¡IntelliSense¡¡is¡¡very¡¡good¡¡and¡¡will¡¡even¡¡display¡¡ments¡¡that¡¡explain¡¡what¡¡the¡¡method¡¡does¡¡£¨as¡¡illustrated¡¡in¡¡¡¡
¡¡¡¡¡¡Figure¡¡3¡2£©¡£¡¡Another¡¡place¡¡to¡¡find¡¡answers¡¡is¡¡the¡¡Microsoft¡¡documentation¡¡itself£»¡¡which¡¡you¡¡can¡¡access¡¡by¡¡¡¡
¡¡¡¡¡¡selecting¡¡Help¡¡¡¡Index¡£¡¡You¡¡can¡¡use¡¡the¡¡Look¡¡For¡¡box¡¡to¡¡search¡¡for¡¡a¡¡specific¡¡type¡£¡¡For¡¡example£»¡¡if¡¡you¡¡type¡¡¡¡
¡¡¡¡¡¡¡°String¡¡class¡±¡¡in¡¡the¡¡Look¡¡For¡¡box£»¡¡you¡¡will¡¡see¡¡details¡¡of¡¡the¡¡String¡¡class£»¡¡which¡¡you¡¡can¡¡then¡¡filter¡¡using¡¡the¡¡¡¡
¡¡¡¡¡¡links¡¡at¡¡the¡¡top¡¡of¡¡the¡¡page¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Microsoft¡¡documentation¡¡is¡¡part¡¡of¡¡the¡¡Microsoft¡¡Developer¡¡Network¡¡£¨MSDN£©¡¡£¨¡¡http£º//¡¡
¡¡¡¡¡¡msdn¡£microsoft¡££©¡£¡¡The¡¡MSDN¡¡web¡¡site¡¡contains¡¡documentation¡¡that¡¡helps¡¡you¡¡figure¡¡out¡¡the¡¡¡¡stan
¡¡¡¡¡¡dard¡¡software¡¡development¡¡kit¡¡£¨¡¡SDK£©¡¡application¡¡programming¡¡interface¡¡£¨API£©¡£¡¡There¡¡are¡¡literally¡¡thou
¡¡¡¡¡¡sands¡¡of¡¡types£»¡¡with¡¡an¡¡explosion¡¡of¡¡methods¡¡and¡¡properties¡£¡¡You¡¡will¡¡definitely¡¡not¡¡use¡¡all¡¡of¡¡them¡¡in¡¡a¡¡single¡¡¡¡
¡¡¡¡¡¡application£»¡¡but¡¡you¡¡will¡¡always¡¡use¡¡the¡¡¡¡SDK¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡most¡¡cases£»¡¡MSDN¡¡will¡¡be¡¡enough¡¡to¡¡help¡¡you¡¡figure¡¡out¡¡when¡¡you¡¡don¡¯t¡¡know¡¡about¡¡a¡¡specific¡¡type¡£¡¡
¡¡¡¡¡¡¡¡If¡¡you¡¡want¡¡to¡¡learn¡¡more¡¡about¡¡concepts£»¡¡you¡¡can¡¡surf¡¡to¡¡a¡¡web¡¡site¡¡such¡¡as¡¡Code¡¡Project¡¡£¨http£º//¡¡
¡¡¡¡¡¡codeproject¡££©¡£¡¡Code¡¡Project¡¡contains¡¡plenty¡¡of¡¡examples¡¡for¡¡almost¡¡every¡¡development¡¡topic¡¡that¡¡¡¡
¡¡¡¡¡¡suits¡¡your¡¡fancy¡£¡¡
The¡¡Problem£º¡¡Character¡by¡Character¡¡parison¡¡
Let¡¯s¡¡get¡¡back¡¡to¡¡the¡¡bug¡¡of¡¡the¡¡whitespace¡£¡¡The¡¡method¡¡that¡¡caused¡¡problems¡¡was¡¡pareTo£¨£©¡£¡¡¡¡
Looking¡¡at¡¡the¡¡MSDN¡¡documentation£»¡¡you¡¡see¡¡the¡¡following¡¡definition¡¡for¡¡this¡¡method¡¡£¨found¡¡¡¡
by¡¡scrolling¡¡down¡¡the¡¡String¡¡class¡¡page¡¡and¡¡clicking¡¡the¡¡pareTo¡¡link£©£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡pares¡¡this¡¡instance¡¡with¡¡a¡¡specified¡¡Object¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡This¡¡definition¡¡does¡¡not¡¡tell¡¡you¡¡much£»¡¡so¡¡you¡¡will¡¡need¡¡to¡¡go¡¡back¡¡to¡¡the¡¡String¡¡page¡¡and¡¡¡¡
click¡¡another¡¡method¡¡name¡£¡¡Click¡¡the¡¡pare¡¡link£»¡¡and¡¡then¡¡click¡¡the¡¡pare£¨String£»¡¡String£©¡¡¡¡
link¡£¡¡In¡¡the¡¡explanation¡¡of¡¡the¡¡pare£¨£©¡¡method£»¡¡you¡¡find¡¡the¡¡following¡¡text£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡The¡¡parison¡¡terminates¡¡when¡¡an¡¡inequality¡¡is¡¡discovered¡¡or¡¡both¡¡strings¡¡have¡¡been¡¡
¡¡¡¡¡¡¡¡¡¡¡¡pared¡£¡¡However£»¡¡if¡¡the¡¡two¡¡strings¡¡pare¡¡equal¡¡to¡¡the¡¡end¡¡of¡¡one¡¡string£»¡¡and¡¡the¡¡
¡¡¡¡¡¡¡¡¡¡¡¡other¡¡¡¡string¡¡¡¡has¡¡¡¡characters¡¡¡¡remaining£»¡¡¡¡then¡¡¡¡the¡¡¡¡string¡¡¡¡with¡¡¡¡remaining¡¡¡¡characters¡¡¡¡is¡¡
¡¡¡¡¡¡¡¡¡¡¡¡considered¡¡greater¡£¡¡The¡¡return¡¡value¡¡is¡¡the¡¡result¡¡of¡¡the¡¡last¡¡parison¡¡performed¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡82¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
60¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡Looking¡¡up¡¡the¡¡meaning¡¡of¡¡a¡¡method¡¡is¡¡not¡¡a¡¡long¡¡process£»¡¡even¡¡though¡¡it¡¡may¡¡seem¡¡like¡¡that¡¡from¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡description¡¡in¡¡this¡¡section¡£¡¡With¡¡experience£»¡¡you¡¡don¡¯t¡¡even¡¡notice¡¡the¡¡extra¡¡clicks¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡pareTo£¨£©¡¡method¡¡failed¡¡because¡¡of¡¡the¡¡character¡by¡character¡¡parison£»¡¡which¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡illustrated¡¡in¡¡Figure¡¡3¡7¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡3¡7¡£¡¡How¡¡pareTo£¨£©¡¡fails¡¡to¡¡pare¡¡strings¡¡that¡¡appear¡¡identical¡¡but¡¡have¡¡extra¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡characters¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Strings¡¡are¡¡stored¡¡in¡¡buffers£»¡¡with¡¡one¡¡space¡¡in¡¡the¡¡buffer¡¡allocated¡¡to¡¡one¡¡character¡£¡¡As¡¡you¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡can¡¡see¡¡from¡¡Figure¡¡3¡7£»¡¡whitespace¡¡takes¡¡up¡¡one¡¡space¡¡in¡¡the¡¡buffer¡£¡¡We¡¡can¡¡take¡¡advantage¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡this¡¡sometimes£»¡¡as¡¡you¡¯ll¡¡see¡¡in¡¡the¡¡next¡¡section¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡that¡¡you¡¡know¡¡what¡¡the¡¡problem¡¡is£»¡¡the¡¡next¡¡step¡¡is¡¡to¡¡find¡¡a¡¡solution¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Solving¡¡the¡¡Extra¡¡Whitespace¡¡Problem¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¡can¡¡solve¡¡the¡¡whitespace¡¡problem¡¡in¡¡multiple¡¡ways¡£¡¡Which¡¡way¡¡you¡¡use¡¡depends¡¡on¡¡your¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡needs¡£¡¡Let¡¯s¡¡look¡¡at¡¡several¡¡solutions¡¡and¡¡see¡¡which¡¡works¡¡best¡¡for¡¡our¡¡translation¡¡program¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Trimming¡¡the¡¡Whitespace¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡solution¡¡we¡¯ll¡¡look¡¡at¡¡is¡¡to¡¡trim¡¡the¡¡whitespace¡¡using¡¡a¡¡method¡¡intended¡¡for¡¡that¡¡purpose¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡whitespace¡¡problem¡¡is¡¡not¡¡unique¡¡and¡¡is¡¡well¡¡known¡£¡¡The¡¡String¡¡type¡¡has¡¡a¡¡method¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡can¡¡be¡¡used¡¡to¡¡remove£»¡¡or¡¡trim£»¡¡the¡¡whitespace¡¡from¡¡a¡¡buffer¡£¡¡You¡¡can¡¡remove¡¡whitespace¡¡at¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡beginning£»¡¡end£»¡¡or¡¡both¡¡sides¡¡of¡¡the¡¡buffer¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡83¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡¡¡61¡¡
¡¡¡¡¡¡¡¡¡¡As¡¡tempting¡¡as¡¡it¡¡is¡¡to¡¡change¡¡the¡¡original¡¡implementation¡¡of¡¡¡¡TranslateHello£¨£©£»¡¡don¡¯t¡¡do¡¡¡¡
that£»¡¡because¡¡you¡¡might¡¡inadvertently¡¡break¡¡something¡¡that¡¡is¡¡working¡¡just¡¡fine¡£¡¡When¡¡you¡¡¡¡
develop¡¡code£»¡¡you¡¡have¡¡multiple¡¡possible¡¡ways¡¡to¡¡solve¡¡a¡¡problem¡£¡¡If¡¡you¡¡start¡¡messing¡¡around¡¡¡¡
with¡¡the¡¡origi
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡