AdvantagesofManagedCodeMicrosoftintermediatelanguageshareswithJavabytecodetheideathatitisalow-levellanguagewithasimplesyntax,whichcanbeveryquicklytranslatedintonativemachinecode.Havingthiswell-defineduniversalsyntaxforcodehassignificantadvantages.PlatformindependenceFirst,itmeansthatthesamefilecontainingbytecodeinstructionscanbeplacedonanyplatform;atruntimethefinalstageofcompilationcanthenbeeasilyaccomplishedsothatthecodewillrunonthatparticularplatform.Inotherwords,bycompilingtoILweobtainplatformindependencefor.NET,inmuchthesamewayascompilingtoJavabytecodegivesJavaplatformindependence.PerformanceimprovementILisactuallyabitmoreambitiousthanJavabytecode.ILisalwaysJust-In-Timecompiled(knownasJIT),whereasJavabytecodewasofteninterpreted.OneofthedisadvantagesofJavawasthat,onexecution,theprocessoftranslatingfromJavabytecodetonativeexecutableresultedinalossofperformance.Insteadofcompilingtheentireapplicationinonego(whichcouldleadtoaslowstart-uptime),theJITcompilersimplycompileseachportionofcodeasitiscalled(just-in-time).Whencodehasbeencompiled.once,theresultantnativeexecutableisstoreduntiltheapplicationexits,sothatitdoesnotneedtoberecompiledthenexttimethatportionofcodeisrun.Microsoftarguesthatthisprocessismoreefficientthancompilingtheentireapplicationcodeatthestart,becauseofthelikelihoodthatlargeportionsofanyapplicationcodewillnotactuallybeexecutedinanygivenrun.UsingtheJITcompiler,suchcodewillneverbecompiled.ThisexplainswhywecanexpectthatexecutionofmanagedILcodewillbealmostasfastasexecutingnativemachinecode.Whatitdoesn’texplainiswhyMicrosoftexpectsthatwewillgetaperformanceimprovement.Thereasongivenforthisisthat,sincethefinalstageofcompilationtakesplaceatruntime,theJITcompilerwillknowexactlywhatprocessortypetheprogramwillrunon.Thismeansthatitcanoptimizethefinalexecutablecodetotakeadvantageofanyfeaturesorparticularmachinecodeinstructionsofferedbythatparticularprocessor.实际上,IL比Java字节代码的作用还要大。IL总是即时编译的(简称JIT),而Java字节代码常常是解释型的,Java的一个缺点是,在运行应用程序时,把Java字节代码转换为内部可执行代码的过程可可能导致性能的损失。JIT编译器并不是把整个应用程序一次编译完(这样会有很长的启动时间),而是只编译它调用的那部分代码。代码编译过一次后,得到的内部可执行代码就存储起来,直到退出该应用程序为止,这样在下次运行这部分代码时,就不需要重新编译了。Microsoft认为这个过程要比一开始就编译整个应用程序代码的效率高得多,因为任何应用程序的大部分代码实际上并不是在每次运行过程中都执行。使用JIT编译器,从来都不会编译这种代码从来都不会被编译。这解释了为什么托管IL代码的执行几乎和内部机器代码的执行速度一样快,但是并没有说明为什么Microsoft认为这会提高性能。其原因是编译过程的最后一部分是在运行时进行的,JIT编译器确切地知道程序运行在什么类型的处理器上,利用该处理器提供的任何特性或特定的机器代码指令来优化最后的可执行代码。传统的编译器会优化代码,但它们的优化过程是独立于代码所运行的特定处理器的。这是因为传统的编译器是在发布软件之前编译为内部机器可执行的代码。即编译器不知道代码所运行的处理器类型,例如该处理器是x86兼容处理器或Alpha处理器,这超出了基本操作的范围。例如VisualStudio6优化了一台一般的Pentium机器,所以它生成的代码就不能利用PentiumIII处理器的硬件特性。相反,JIT编译器不仅可以进行VisualStudio6所能完成的优化工作,还可以优化代码所运行的特定处理器。Traditionalcompilerswilloptimizethecode,buttheycanonlyperformoptimizationsthatareindependentoftheparticularprocessorthatthecodewillrunon.Thisisbecausetraditionalcompilerscompiletonativeexecutablebeforethesoftwareisshipped.Thismeansthatthecompilerdoesn’tknowwhattypeofprocessorthecodewillrunonbeyondbasicgeneralities,suchasthatitwillbeanx86-compatibleprocessororanAlphaprocessor.VisualStudio6,forexample,optimizesforagenericPentiummachine,sothecodethatitgeneratescannottakeadvantageofhardwarefeaturesofPentiumIIIprocessors.Ontheotherhand,theJITcompilercandoalltheoptimizationsthatVisualStudio6can,andinadditionitwilloptimizefortheparticularprocessorthecodeisrunningon.LanguageinteroperabilityTheuseofILnotonlyenablesplatformindependence;italsofacilitateslanguageinteroperability.Simplyput,youcancompiletoILfromonelanguage,andthiscompiledcodeshouldthenbeinteroperablewithcodethathasbeencompiledtoILfromanotherlanguage.You’reprobablynowwonderingwhichlanguagesasidefromC#areinteroperablewith.NET,solet’sbrieflydiscusshowsomeoftheothercommonlanguagesfitinto.NET.VisualBasic.NETVisualBasic.NEThasundergoneacompleterevampfromVisualBasic6tobringitup-to-datewith.NET.ThewaythatVisualBasichasevolvedoverthelastfewyearsmeansthatinitspreviousversion,VisualBasic6,itwasnotasuitablelanguageforrunning.NETprograms.Forexample,itisheavilyintegratedintoCOMandworksbyexposingonlyeventhandlersassourcecodetothedeveloper—mostofthebackgroundcodeisnotavailableassourcecode.Notonlythat,itdoesnotsupportimplementationinheritance,andthestandarddatatypesVisualBasic6usesareincompatiblewith.NET.VisualBasic6wasupgradedtoVisualBasic.NET,andthechangesthatweremadetothelanguagearesoextensiveyoumightaswellregardVisualBasic.NETasanewlanguage.ExistingVisualBasic6codedoesnotcompileasVisualBasic.NETcode.ConvertingaVisualBasic6programtoVisualBasic.NETrequiresextensivechangestothecode.However,VisualStudio.NET(theupgradeofVSforusewith.NET)candomostofthechangesforyou.IfyouattempttoreadaVisualBasic6projectintoVisualStudio.NET,itwillupgradetheprojectforyou,whichmeansthatitwillrewritetheVisualBasic6sourcecodeintoVisualBasic.NETsourcecode.Althoughthismeansthattheworkinvolvedforyouisheavilycutdown,youwillneedtocheckthroughthenewVisualBasic.NETcodetomakesurethattheprojectstillworksasintendedbecausetheconversionmightnotbeperfect.OnesideeffectofthislanguageupgradeisthatitisnolongerpossibletocompileVisualBasic.NETtonativeexecutablec