0

I know the CLR would optimize some codes, but I don't know what are they, like the code below:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Program p = new Program();
            string another = "a";
            var no = p.SNo;
            var field = p.FieldA;
            string name = "stackoverflow" + p.Name;
            var a = p.Name;
        }
        catch (Exception ex)
        {
            Console.WriteLine("yes, clr runs it");
        }
        Console.WriteLine("over");
    }

    public string FieldA;

    public string Name
    {
        get
        {
            return GetName();
        }
    }

    public string SNo
    {
        get;
        set;
    }

    public string GetName()
    {
        throw new Exception("can you run at here?");
    }
}

environment: .net 4.0 + vs2015 + win7x64 output in debug mode(it runstring another = "a";):

yes, clr runs it

over

output in release mode(the same as debug mode), and it has the 'Optimize code' flag:

yes, clr runs it

over

then I find the code in dll:

private static void Main(string[] args)
{
    try
    {
        Program program = new Program();
        string sNo = program.SNo;
        string fieldA = program.FieldA;
        string text3 = "stackoverflow" + program.Name;
        string name = program.Name;
    }
    catch (Exception)
    {
        Console.WriteLine("yes, clr runs it");
    }
    Console.WriteLine("over");
}

the string another = "a"; has disappeared, but the property program.SNo and the field program.FieldA and the nonuse text3 are there.

So, what has the clr done? and will it be different when it become an aps.net application running in the IIS? has the IIS done something?

Community
  • 1
  • 1
windc
  • 131
  • 6

1 Answers1

1

Removal of dead code, including unused variables, is one of the most basic optimizations any compiler does. Code analysis raises a warning for this: CA1804: Remove unused locals and it very explicitly states:

Note that the C# compiler [...] removes unused local variables when the optimize option is enabled.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • thank you first, but program.SNo and program.FieldA are also not used, why they are in the dll? if the code in dll is the real code to be running? – windc Jul 06 '16 at 08:28
  • 1
    Because those are not locals. Unused locals are guaranteed to be unreachable, but unused fields inside a DLL may well be consumed by another app referencing this dll of yours. No offense, but you are barely skimming the surface of a huge body of knowledge that I feel you are yet to master. You can read and learn plenty about compiler optimization. Here is something short to wet your appetite: [What does the optimize switch do?](https://blogs.msdn.microsoft.com/ericlippert/2009/06/11/what-does-the-optimize-switch-do/) – Remus Rusanu Jul 06 '16 at 08:51
  • thanks again, I read that articel and know why it do as this a little, now my question are: 1:are there only clr compile and jitter to optimize? 2:if 1 is true, there is no way to find out what it is real running code? I am wondering how my programe works. – windc Jul 06 '16 at 10:15