c#编译器的减少冗余代码优化

MSIL(微软中间语言)的反编译工具(ildasm.exe)[VS2005自带],所以特意用来检测了c#的语言编译功能,首先介绍下减少冗余代码功能。

 

源代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace enviroment
{
    class Program
    {
        static void Main(string[] args)
        {
            const string hello = "HELLOWORLD";
            Console.WriteLine(hello);
            Console.Read();
        }
    }
}

【反编译结果】

.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size       19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldstr      "HELLOWORLD"
IL_0006: call       void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: call       int32 [mscorlib]System.Console::Read()
IL_0011: pop
IL_0012: ret
} // end of method Program::Main

 

【修改后】

using System;
using System.Collections.Generic;
using System.Text;

namespace enviroment
{
    class Program
    {
        static void Main(string[] args)
        {
            const string hello = "HELLOWORLD";
            //Console.WriteLine(hello);被注释了!
            Console.Read();
        }
    }
}

信息提醒:Warning 1 The variable 'hello' is assigned but its value is never used C:/Documents and Settings/01/桌面/dotnet/enviroment/enviroment/Program.cs 11 26 enviroment

【反编译结果】

.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size       8 (0x8)
.maxstack 8
IL_0000: nop
IL_0001: call       int32 [mscorlib]System.Console::Read()
IL_0006: pop
IL_0007: ret
} // end of method Program::Main

强大的VS编译器已经把中间的冗余代码去掉了!

你可能感兴趣的:(c#编译器的减少冗余代码优化)