mercredi 6 juillet 2016

if the code in C# dll is running?

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?





Aucun commentaire:

Enregistrer un commentaire