ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
¸»Ê¿¿µÐ¡ËµÍø ·µ»Ø±¾ÊéĿ¼ ¼ÓÈëÊéÇ© ÎÒµÄÊé¼Ü ÎÒµÄÊéÇ© TXTÈ«±¾ÏÂÔØ ¡ºÊղص½ÎÒµÄä¯ÀÀÆ÷¡»

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ10²¿·Ö

¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Go¡¡ahead¡¡and¡¡add¡¡the¡¡TestCalculator¡¡project¡¡to¡¡the¡¡Calculator¡¡solution¡£¡¡Remember¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡add¡¡a¡¡reference¡¡to¡¡the¡¡Calculator¡¡class¡¡library¡¡£¨right¡­click¡¡TestCalculator¡¡and¡¡choose¡¡Add¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Reference¡¡¡¡Projects¡¡¡¡Calculator£©¡£¡¡Remember¡¡to¡¡set¡¡TestCalculator¡¡as¡¡the¡¡startup¡¡project¡¡for¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡debugging¡¡purposes¡£¡¡Figure¡¡2¡­10¡¡shows¡¡the¡¡TestCalculator¡¡and¡¡Calculator¡¡projects¡¡in¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Solution¡¡Explorer¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡59¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡37¡¡



Figure¡¡2¡­10¡£¡¡Solution¡¡Explorer¡¡showing¡¡the¡¡testing¡¡console¡¡application¡¡and¡¡Calculator¡¡¡¡

class¡¡library¡¡



Testing¡¡Simple¡¡Addition¡¡



Add¡¡the¡¡boldfaced¡¡code¡¡to¡¡Module1¡£vb¡¡in¡¡the¡¡testing¡¡console¡¡project¡¡to¡¡verify¡¡the¡¡addition¡¡of¡¡1¡¡¡¡

and¡¡2¡¡£¨the¡¡¡¡operator¡¡means¡¡is¡¡not¡¡equal¡¡to£©£º¡¡



Imports¡¡Calculator¡¡



Module¡¡Module1¡¡

¡¡¡¡¡¡¡¡Public¡¡Sub¡¡TestSimpleAddition£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡result¡¡As¡¡Integer¡¡=¡¡Operations¡£Add£¨1£»¡¡2£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨total¡¡¡¡3£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Oops¡¡1¡¡and¡¡2¡¡does¡¡not¡¡equal¡¡3¡¨£©¡¡

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

¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡Sub¡¡Main£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestSimpleAddition£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£ReadKey£¨£©¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡



End¡¡Module¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡60¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

38¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Press¡¡Ctrl£«F5¡¡to¡¡test¡¡the¡¡calculation¡£¡¡When¡¡executed£»¡¡the¡¡testing¡¡console¡¡application¡¡calls¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡test¡¡method¡¡TestSimpleAddition£¨£©£»¡¡which¡¡calls¡¡and¡¡verifies¡¡the¡¡functionality¡¡of¡¡the¡¡Calculator¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡class¡¡library¡£¡¡If¡¡everything¡¡works£»¡¡the¡¡message¡¡will¡¡not¡¡appear¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡Recall¡¡that¡¡the¡¡Main£¨£©¡¡method¡¡is¡¡where¡¡a¡¡project¡¡begins¡¡its¡¡operation¡£¡¡To¡¡make¡¡an¡¡application¡¡do¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡something£»¡¡you¡¡must¡¡add¡¡code¡¡to¡¡its¡¡Main£¨£©¡¡method¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡see¡¡that¡¡the¡¡test¡¡also¡¡fails£»¡¡change¡¡the¡¡Add£¨£©¡¡method¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡Function¡¡Add£¨ByVal¡¡number1¡¡As¡¡Integer£»¡¡ByVal¡¡number2¡¡As¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer£©¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡number1¡¡*¡¡number2¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡rerun¡¡the¡¡program£»¡¡and¡¡you¡¯ll¡¡see¡¡the¡¡failure¡¡message¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡THE¡¡DEVELOPMENT¡¡CYCLE¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡So¡¡far¡¡in¡¡this¡¡chapter£»¡¡we¡¯ve¡¡dealt¡¡with¡¡three¡¡pieces¡¡of¡¡code£º¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡¡¡The¡¡code¡¡segment¡¡that¡¡implements¡¡the¡¡Add£¨£©¡¡method¡¡performs¡¡the¡¡calculation¡¡operation¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡¡¡The¡¡code¡¡that¡¡represents¡¡a¡¡caller£»¡¡which¡¡could¡¡be¡¡either¡¡the¡¡Windows¡¡application¡¡or¡¡the¡¡console¡¡appli

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cation£»¡¡is¡¡considered¡¡production¡¡code¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡¡¡The¡¡code¡¡that¡¡contains¡¡the¡¡production¡¡code¡¡with¡¡some¡¡verification¡¡routines¡¡represents¡¡testing¡¡code¡£¡¡The¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡testing¡¡code¡¡is¡¡important¡¡because£»¡¡if¡¡the¡¡ponent¡¡implementation¡¡is¡¡altered£»¡¡you¡¡need¡¡to¡¡rerun¡¡only¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡testing¡¡code¡¡to¡¡make¡¡sure¡¡everything¡¡works¡¡as¡¡it¡¡should¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡These¡¡three¡¡pieces¡¡of¡¡code¡¡have¡¡demonstrated¡¡a¡¡plete¡¡development¡¡cycle¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Testing¡¡the¡¡Addition¡¡of¡¡Two¡¡Very¡¡Large¡¡Numbers¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡code¡¡and¡¡projects¡¡have¡¡been¡¡organized£»¡¡but¡¡we¡¯re¡¡missing¡¡some¡¡tests¡£¡¡The¡¡current¡¡test¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡adds¡¡two¡¡simple¡¡numbers¡£¡¡Another¡¡test¡¡could¡¡be¡¡to¡¡add¡¡two¡¡really¡¡big¡¡numbers£»¡¡such¡¡as¡¡2¡¡billion¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡2¡¡billion¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡code¡¡to¡¡test¡¡the¡¡addition¡¡of¡¡two¡¡really¡¡big¡¡numbers¡¡is¡¡shown¡¡in¡¡Figure¡¡2¡­11¡£¡¡Add¡¡this¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Module1¡£vb¡¡in¡¡the¡¡TestCalculator¡¡project¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡61¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡39¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Addition¡¡result¡¡is¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡stored¡¡in¡¡a¡¡variable¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡¡billion¡¡added¡¡to¡¡2¡¡billion¡¡



¡¡¡¡Public¡¡Sub¡¡TestReallyBigNumbers£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡total¡¡As¡¡Integer¡¡=¡¡Operations¡£Add£¨2000000000£»¡¡2000000000£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨total¡¡¡¡4000000000£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Error¡¡found£¨¡¨¡¡&¡¡total¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¨£©¡¡should¡¡have¡¡been¡¡£¨4000000000£©¡¨£©¡¡

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

¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Verification¡¡of¡¡addition¡¡is¡¡4¡¡billion¡¡¡¡¡¡¡¡If¡¡there¡¡is¡¡an¡¡error£»¡¡a¡¡string¡¡is¡¡built¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡using¡¡concatenation¡¡techniques¡¡



Figure¡¡2¡­11¡£¡¡¡¡Testing¡¡the¡¡addition¡¡of¡¡two¡¡large¡¡numbers¡¡



¡¡¡¡¡¡¡¡¡¡¡¡The¡¡test¡¡to¡¡add¡¡two¡¡very¡¡large¡¡numbers¡¡is¡¡identical¡¡to¡¡the¡¡test¡¡to¡¡add¡¡two¡¡simple¡¡numbers£»¡¡¡¡

except¡¡for¡¡the¡¡numbers¡¡themselves¡£¡¡The¡¡error¡¡message¡¡is¡¡handled¡¡a¡¡bit¡¡differently¡¡than¡¡in¡¡the¡¡¡¡

previous¡¡code£»¡¡in¡¡that¡¡it¡¡is¡¡built¡¡using¡¡concatenation¡¡techniques¡£¡¡In¡¡the¡¡example£»¡¡a¡¡string¡¡is¡¡¡¡

concatenated¡¡with¡¡an¡¡integer¡¡with¡¡a¡¡string¡£¡¡Visual¡¡Basic¡¡will¡¡automatically¡¡convert¡¡the¡¡integer¡¡¡¡

into¡¡a¡¡string¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Often£»¡¡you¡¡will¡¡write¡¡tests¡¡where¡¡the¡¡only¡¡real¡¡difference¡¡is¡¡the¡¡data¡¡itself£»¡¡not¡¡the¡¡ramifica

tions¡¡of¡¡the¡¡data¡£¡¡Do¡¡you¡¡think¡¡of¡¡adding¡¡two¡¡very¡¡large¡¡numbers¡¡£¨such¡¡as¡¡2¡¡billion¡¡plus¡¡2¡¡billion£©¡¡¡¡

and¡¡two¡¡smaller¡¡numbers¡¡£¨such¡¡as¡¡2¡¡plus¡¡2£©¡¡as¡¡different£¿¡¡No£»¡¡because¡¡to¡¡humans£»¡¡the¡¡major¡¡¡¡

difference¡¡between¡¡2¡¡billion¡¡and¡¡2¡¡is¡¡a¡¡bunch¡¡of¡¡zeros£»¡¡the¡¡result¡¡is¡¡either¡¡4¡¡billion¡¡or¡¡4£»¡¡which¡¡¡¡

seems¡¡very¡¡trivial¡£¡¡Yet¡¡with¡¡respect¡¡to¡¡a¡¡puter£»¡¡4¡¡billion¡¡is¡¡very¡¡different¡¡from¡¡4£»¡¡as¡¡you¡¯ll¡¡¡¡

soon¡¡see¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Before¡¡you¡¡run¡¡the¡¡test£»¡¡you¡¡need¡¡to¡¡add¡¡a¡¡call¡¡to¡¡it¡¡in¡¡the¡¡Main£¨£©¡¡method£º¡¡



¡¡¡¡¡¡¡¡Sub¡¡Main£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestSimpleAddition£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestReallyBigNumbers£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£ReadKey£¨£©¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡run¡¡the¡¡very¡¡large¡¡number¡¡test¡£¡¡You¡¡will¡¡see¡¡the¡¡output¡¡shown¡¡in¡¡Figure¡¡2¡­12¡£¡¡

¡¡¡¡¡¡¡¡¡¡What¡¡happened£¿¡¡The¡¡problem¡¡has¡¡to¡¡do¡¡with¡¡the¡¡addition¡¡of¡¡very¡¡large¡¡numbers¡¡that¡¡cause¡¡¡¡

an¡¡overflow£»¡¡which¡¡is¡¡a¡¡puter¡¡construct¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡62¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

40¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡2¡­12¡£¡¡¡¡Visual¡¡Basic¡¡Express¡¡highlighting¡¡the¡¡overflow¡¡error¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Understanding¡¡Problems¡¡with¡¡Numeric¡¡Numbers¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡What¡¡you¡¡understand¡¡as¡¡numbers¡¡and¡¡what¡¡the¡¡puter¡¡understands¡¡as¡¡numbers¡¡are¡¡two¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡pletely¡¡different¡¡things¡£¡¡As¡¡a¡¡child£»¡¡you¡¡learn¡¡to¡¡count¡¡starting¡¡at¡¡1¡¡and¡¡ending¡¡at¡¡100£»¡¡which¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡thought¡¡was¡¡a¡¡gargantuan¡¡number¡£¡¡As¡¡you¡¡grew¡¡up£»¡¡you¡¡learned¡¡about¡¡zero¡¡and¡¡numbers¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡less¡¡than¡¡zero¡£¡¡Later£»¡¡you¡¡advanced¡¡to¡¡studying¡¡fractions¡¡and¡¡decimal¡¡values¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throughout¡¡all¡¡of¡¡this¡¡learning¡¡about¡¡numbers£»¡¡you¡¡considered¡¡the¡¡number¡¡1¡¡and¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡number¡¡1¡£5¡¡to¡¡be¡¡the¡¡same¡¡kind¡¡of¡¡thing¡ªthat¡¡is£»¡¡numbers¡£¡¡Yet¡¡to¡¡the¡¡puter£»¡¡they¡¡are¡¡not¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡same¡¡type¡¡of¡¡values¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡reason¡¡numbers¡¡are¡¡different¡¡to¡¡puters¡¡has¡¡to¡¡do¡¡with¡¡puter¡¡efficiency¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡how¡¡the¡¡puter¡¡stores¡¡numbers¡£¡¡For¡¡those¡¡who¡¡have¡¡learned¡¡the¡¡decimal¡¡system¡¡with¡¡place

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡holders£»¡¡you¡¡start¡¡counting¡¡at¡¡0£»¡¡continue¡¡to¡¡9£»¡¡and¡¡then¡¡the¡¡next¡¡number¡¡is¡¡a¡¡decimal¡¡increment¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡called¡¡10¡£¡¡The¡¡decimal¡¡system¡¡is¡¡originally¡¡accredited¡¡to¡¡the¡¡Babylonians£»¡¡but¡¡they¡¡used¡¡a¡¡decimal¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡system¡¡that¡¡was¡¡sexagesimal¡¡£¨60¡¡unique¡¡identifiers¡¡versus¡¡our¡¡decimal¡¡10¡¡unique¡¡identifiers£©¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡puter¡¡has¡¡a¡¡similar¡¡counting¡¡scheme£»¡¡except¡¡it¡¡is¡¡binary¡¡with¡¡only¡¡two¡¡unique¡¡identi

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡fiers£º¡¡1¡¡and¡¡0¡£¡¡puters¡¡have¡¡two¡¡unique¡¡identifiers¡¡because¡¡they¡¡signify¡¡two¡¡unique¡¡states£º¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡on¡¡and¡¡off¡£¡¡At¡¡the¡¡heart¡¡of¡¡a¡¡puter¡¡is¡¡a¡¡transistor¡¡that¡¡has¡¡the¡¡ability¡¡to¡¡distinguish¡¡between¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡on¡¡and¡¡off£»¡¡there¡¡are¡¡no¡¡¡°sort¡¡of¡±¡¡on¡¡and¡¡¡°sort¡¡of¡±¡¡off¡¡states¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡2¡­13¡¡shows¡¡an¡¡example¡¡of¡¡how¡¡a¡¡puter¡¡would¡¡count¡¡to¡¡7¡¡using¡¡the¡¡binary¡¡system¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡theory£»¡¡you¡¡could¡¡count¡¡until¡¡you¡¡die¡£¡¡You¡¡have¡¡that¡¡ability¡¡because¡¡you¡¡are¡¡human¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡have¡¡no¡¡limits¡¡on¡¡how¡¡high¡¡you¡¡can¡¡count¡£¡¡A¡¡puter¡¡does¡¡have¡¡limits£»¡¡such¡¡as¡¡random¡­access¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡memory¡¡£¨RAM£©£»¡¡hard¡¡disk£»¡¡and¡¡so¡¡on¡£¡¡Specific¡¡numeric¡¡data¡¡types¡¡on¡¡a¡¡puter¡¡also¡¡have¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡limits¡¡on¡¡how¡¡high¡¡they¡¡can¡¡count¡£¡¡For¡¡example£»¡¡the¡¡Integer¡¡data¡¡type¡¡that¡¡we¡¡have¡¡been¡¡using¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡can¡¡count¡¡to¡¡only¡¡a¡¡specific¡¡number¡¡and¡¡can¡¡store¡¡only¡¡whole¡¡numbers¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Think¡¡of¡¡the¡¡different¡¡numeric¡¡data¡¡types¡¡as¡¡odometers¡£¡¡Most¡¡car¡¡odometers¡¡have¡¡upper¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡limits¡¡of¡¡1¡¡million¡¡miles/kilometers¡£¡¡Imagine¡¡adding¡¡900£»000¡¡and¡¡200£»000¡¡using¡¡an¡¡odometer¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡result¡¡would¡¡be¡¡100£»000£»¡¡and¡¡not¡¡the¡¡1¡£1¡¡million¡¡that¡¡you¡¡expected¡£¡¡This¡¡is¡¡exactly¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡problem¡¡that¡¡we¡¡encountered¡¡when¡¡adding¡¡2¡¡billion¡¡and¡¡2¡¡billion¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡63¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡41¡¡



Figure¡¡2¡­13¡£¡¡How¡¡a¡¡puter¡¡counts¡¡to¡¡7¡¡



¡¡¡¡¡¡¡¡¡¡¡¡The¡¡bad¡¡part¡¡of¡¡an¡¡odometer¡¡is¡¡that¡¡you¡¡don¡¯t¡¡know¡¡when¡¡the¡¡odometer¡¡has¡¡been¡¡wrapped¡¡¡¡

around¡£¡¡You¡¡could¡¡end¡¡up¡¡with¡¡a¡¡car¡¡whose¡¡odometer¡¡read¡¡100£»000¡¡miles/kilometers£»¡¡but¡¡actu

ally¡¡had¡¡driven¡¡1¡£1¡¡million¡£¡¡Fortunately£»¡¡¡¡knows¡¡when¡¡the¡¡number¡¡type¡¡has¡¡been¡¡wrapped¡¡¡¡

around¡£¡¡The¡¡technical¡¡jargon¡¡for¡¡this¡¡situation¡¡is¡¡overflow¡¡or¡¡¡¡underflow¡£¡¡Overflow¡¡is¡¡when¡¡the¡¡¡¡

odometer¡¡wraps¡¡over¡¡in¡¡the¡¡positive¡¡direction¡¡£¨900£»000¡¡to¡¡1¡£1¡¡million£©£»¡¡and¡¡underflow¡¡is¡¡when¡¡¡¡

the¡¡odometer¡¡wraps¡¡around¡¡in¡¡the¡¡negative¡¡direction¡¡£¨0¡¡to¡¡¨C100£»000£©¡£¡¡Detection¡¡of¡¡either¡¡situa

tion¡¡is¡¡activated¡¡as¡¡a¡¡project¡¡property¡£¡¡Follow¡¡these¡¡steps¡¡to¡¡deactivate¡¡overflow/underflow¡¡¡¡

detection¡¡for¡¡the¡¡Calculator¡¡class¡¡library£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡Right¡­click¡¡the¡¡Calculator¡¡project¡¡in¡¡the¡¡Calculator¡¡solution¡¡and¡¡select¡¡Properties¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡Click¡¡pile£»¡¡and¡¡then¡¡click¡¡Advanced¡¡pile¡¡Options¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡3¡£¡¡Check¡¡¡°Remove¡¡integer¡¡overflow¡¡tests¡±¡¡to¡¡remove¡¡the¡¡test¡¡for¡¡overflow¡¡situations¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡4¡£¡¡Click¡¡OK¡¡to¡¡finish¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡Rerun¡¡the¡¡test¡¡console¡¡application£»¡¡and¡¡you¡¡will¡¡get¡¡an¡¡exception¡¡indicating¡¡that¡¡an¡¡over

flow¡¡has¡¡occurred¡£¡¡



Error¡¡found£¨¡­294967296£©¡¡should¡¡have¡¡been¡¡£¨4000000000£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡Having¡¡an¡¡overflow¡¡situation¡¡is¡¡a¡¡problem£»¡¡and¡¡the¡¡fact¡¡that¡¡¡¡can¡¡catch¡¡it¡¡is¡¡a¡¡good¡¡¡¡

thing¡£¡¡But¡¡at¡¡the¡¡end¡¡of¡¡the¡¡day£»¡¡our¡¡preferred¡¡solution¡¡is¡¡to¡¡be¡¡able¡¡to¡¡add¡¡2¡¡billion¡¡plus¡¡2¡¡billion¡£¡¡¡¡

After¡¡all£»¡¡Bill¡¡Gates¡¡would¡¡probably¡¡prefer¡¡having¡¡4¡¡billion¡¡in¡¡his¡¡bank¡¡account¡¡instead¡¡of¡¡the¡¡¡¡

calculated¡¡minus¡¡value¡¡or¡¡an¡¡error¡¡indicating¡¡that¡¡the¡¡bank¡¡cannot¡¡accept¡¡his¡¡4¡¡billion¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡64¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

42¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Understanding¡¡Numeric¡¡and¡¡Value¡¡Data¡¡Types¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡A¡¡¡¡data¡¡type¡¡is¡¡a¡¡way¡¡of¡¡describing¡¡a¡¡piece¡¡of¡¡data¡¡using¡¡a¡¡metadescription¡£¡¡For¡¡example£»¡¡if¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡have¡¡a¡¡Double¡¡type£»¡¡you¡¡know¡¡you¡¡will¡¡have¡¡a¡¡double¡¡number¡£¡¡Many¡¡data¡¡types¡¡are¡¡available£º¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer£»¡¡Long£»¡¡Short£»¡¡Single£»¡¡Double£»¡¡String£»¡¡Enum£»¡¡Structure£»¡¡and¡¡so¡¡on¡£¡¡You¡¡can¡¡even¡¡define¡¡your¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡own¡¡data¡¡types¡£¡¡Data¡¡types¡¡are¡¡the¡¡heart¡¡of¡¡the¡¡CLR¡¡and¡¡a¡¡type¡­safe¡¡programming¡¡environment¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Understanding¡¡Value¡¡and¡¡Reference¡¡Types¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡CLR¡¡supports¡¡two¡¡ways¡¡of¡¡representing¡¡data£º¡¡value¡¡and¡¡reference¡£¡¡The¡¡major¡¡difference¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡between¡¡a¡¡value¡¡and¡¡reference¡¡type¡¡is¡¡how¡¡the¡¡information¡¡associated¡¡with¡¡the¡¡type¡¡is¡¡stored¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡problem¡¡with¡¡value¡¡and¡¡reference¡¡types¡¡is¡¡that¡¡they¡¡are¡¡a¡¡technical¡¡invention¡¡and¡¡can¡¡be¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡confusing¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡the¡¡CLR¡¡runs¡¡an¡¡application£»¡¡a¡¡thread¡¡is¡¡executing¡¡the¡¡mon¡¡Language¡¡Infra

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡structure¡¡£¨CLI£©¡£¡¡Think¡¡of¡¡a¡¡thread¡¡as¡¡you¡¡wandering¡¡around¡¡a¡¡mall¡¡buying¡¡things¡£¡¡You¡¡are¡¡an¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡individual¡¡and¡¡can¡¡buy¡¡things¡¡independently¡¡of¡¡other¡¡people¡£¡¡The¡¡store¡¡will¡¡have¡¡many¡¡people¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡looking¡¡around¡¡and¡¡buying¡¡different¡¡things¡£¡¡Likewise£»¡¡a¡¡puter¡¡has¡¡many¡¡threads¡¡doing¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡many¡¡different¡¡independent¡¡things¡£¡¡When¡¡you¡¡look¡¡around¡¡in¡¡a¡¡store£»¡¡you¡¡might¡¡bump¡¡into¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡people¡¡and¡¡cause¡¡them¡¡to¡¡drop¡¡things¡£¡¡While¡¡the¡¡CLR¡¡tries¡¡to¡¡avoid¡¡such¡¡problems£»¡¡if¡¡you¡¡try¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡hard¡¡enough¡¡in¡¡your¡¡code£»¡¡you¡¡can¡¡cause¡¡other¡¡threads¡¡to¡¡¡°drop¡±¡¡things¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡it¡¡executes£»¡¡a¡¡thread¡¡has¡¡a¡¡local¡¡memory¡¡pool¡¡called¡¡a¡¡stack£»¡¡which¡¡is¡¡akin¡¡to¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡carrying¡¡a¡¡wallet¡¡that¡¡contains¡¡cash¡¡and¡¡credit¡¡cards¡£¡¡You¡¡carry¡¡the¡¡wallet¡¡with¡¡you¡¡from¡¡store¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡store£»¡¡like¡¡a¡¡thread¡¡carries¡¡a¡¡stack¡¡when¡¡calling¡¡one¡¡method¡¡to¡¡another¡£¡¡When¡¡you¡¡enter¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡store¡¡and¡¡want¡¡to¡¡purchase¡¡something£»¡¡you¡¡have¡¡two¡¡major¡¡ways¡¡of¡¡paying¡¡for¡¡the¡¡item£º¡¡with¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cash¡¡or¡¡by¡¡using¡¡a¡¡credit/debit¡¡card¡£¡¡However£»¡¡with¡¡a¡¡credit/debit¡¡card£»¡¡you¡¡can¡¯t¡¡pay¡¡immedi

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ately¡£¡¡You¡¡need¡¡a¡¡machine¡¡that¡¡calls¡¡a¡¡server¡¡to¡¡verify¡¡that¡¡your¡¡piece¡¡of¡¡plastic¡¡has¡¡enough¡¡money¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡pay¡¡for¡¡the¡¡item¡£¡¡Paying¡¡with¡¡cash¡¡is¡¡much¡¡faster¡¡than¡¡paying¡¡with¡¡a¡¡credit¡¡card£»¡¡because¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡don¡¯t¡¡need¡¡to¡¡talk¡¡to¡¡a¡¡remote¡¡puter¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡suppose¡¡you¡¡and¡¡your¡¡spouse¡¡want¡¡to¡¡pay¡¡for¡¡the¡¡item¡£¡¡You¡¡could¡¡use¡¡the¡¡same¡¡credit¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡card¡¡account£»¡¡but¡¡you¡¡have¡¡unique¡¡credit¡¡cards¡£¡¡But¡¡you¡¡cannot¡¡do¡¡the¡¡same¡¡thing¡¡with¡¡cash¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡you¡¡have¡¡a¡¡10¡¡bill£»¡¡your¡¡spouse¡¡cannot¡¡share¡¡the¡¡10¡¡with¡¡you¡£¡¡Your¡¡spouse¡¡would¡¡need¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡second¡¡10¡¡bill£»¡¡and¡¡together¡¡you¡¡would¡¡have¡¡20¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡cash¡¡and¡¡credit¡¡card¡¡payment¡¡methods¡¡are¡¡analogous¡¡to¡¡value¡¡and¡¡reference¡¡types¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Cash¡¡is¡¡a¡¡value¡¡type£»¡¡and¡¡the¡¡credit¡¡card¡¡is¡¡a¡¡reference¡¡type¡£¡¡When¡¡the¡¡CLR¡¡executes£»¡¡the¡¡code¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡dragged¡¡from¡¡one¡¡method¡¡call¡¡to¡¡another¡¡method¡¡call¡¡is¡¡the¡¡stack¡¡that¡¡contains¡¡a¡¡number¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡type¡¡variables¡£¡¡Value¡¡types¡¡are¡¡stored¡¡directly¡¡on¡¡the¡¡stack¡¡like¡¡cash¡£¡¡Reference¡¡types¡¡are¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡stored¡¡as¡¡pointers¡¡to¡¡memory¡¡on¡¡the¡¡stack£»¡¡just¡¡like¡¡a¡¡credit/debit¡¡card¡¡points¡¡to¡¡cash¡¡some

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡where¡¡else¡£¡¡The¡¡reference¡¡pointer¡¡points¡¡to¡¡a¡¡piece¡¡of¡¡memory¡¡called¡¡the¡¡¡¡heap¡£¡¡These¡¡concepts¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡are¡¡illustrated¡¡in¡¡Figure¡¡2¡­14¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡With¡¡value¡¡types£»¡¡when¡¡one¡¡value¡¡type¡¡is¡¡assigned¡¡to¡¡another£»¡¡the¡¡contents¡¡are¡¡copied¡£¡¡If¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡modify¡¡one¡¡of¡¡the¡¡copies£»¡¡the¡¡original¡¡will¡¡not¡¡be¡¡altered¡£¡¡In¡¡contrast£»¡¡when¡¡you¡¡change¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡of¡¡a¡¡reference¡¡type£»¡¡the¡¡values¡¡of¡¡all¡¡the¡¡pointers¡¡to¡¡that¡¡reference¡¡type¡¡change¡£¡¡Going¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡back¡¡to¡¡the¡¡credit¡¡card¡¡and¡¡cash¡¡example£»¡¡if¡¡you¡¡have¡¡10¡¡and¡¡so¡¡does¡¡your¡¡spouse£»¡¡then¡¡when¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡spend¡¡8£»¡¡it¡¡does¡¡not¡¡affect¡¡the¡¡10¡¡that¡¡your¡¡spouse¡¡has£»¡¡as¡¡befits¡¡the¡¡value¡¡type¡¡model¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Howev
·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨11£© ²È£¨11£©
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾­Ñé½±Àø£¬ÈÏÕæдԭ´´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾­Ñé½±ÀøŶ£¡