Max Levy
Max Levy

Reputation: 404

Unexpected behavior for static init in LINQPad

I run the following LINQPad's C# Program twice:

void Main()
{
    new Bar();
}

class Bar
{
    static DateTime staticInitTime = DateTime.Now;
    DateTime initTime = DateTime.Now;

    public Bar()
    {
        staticInitTime.Dump("staticInitTime");
        initTime.Dump("initTime");
    }
}

and while I see no problems with the results of the first run:

staticInitTime
3/4/2017 11:45:28 PM 

initTime
3/4/2017 11:45:28 PM 

I can't understanding the results of the second run, just a few seconds after the first:

staticInitTime
3/4/2017 11:45:28 PM 

initTime
3/4/2017 11:45:40 PM

Specifically: why staticInitTime keeps it's value between the runs? Only when I re-build the code, the value of staticInitTime gets initialized, but then only for the first run.

Upvotes: 1

Views: 101

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109255

That's because Linqpad is smart. By default, it doesn't build a new AppDomain each time you run your query. So any static initialization will be preserved until you rebuild. You can change this behavior in the advanced preferences:

enter image description here

By the way, totally off topic: Linqpad rocks!

Upvotes: 2

Related Questions