V
Vagif Abilov
I investigate further the problem I reported earlier as incorrectly set
values of static variable instances. Actually it is reproduced with a little
piece of code.
I made a simlpe Web site consisting of an empty page. The page C# code file
looks like this:
using System;
using System.Web;
namespace StaticMemberTest
{
public class Test
{
static int A
{
get { _Default.a++; return _Default.a; }
}
}
public partial class _Default : System.Web.UI.Page
{
internal static int a = 0;
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("A = " + a.ToString());
}
}
}
Steps to reproduce the problem:
1. Set the breakpoint within Page_Load
2. Start the debugger.
3. Once the breakpoint is reached, go to Watch window and type "Test.A".
4. This will trigger evaluation of Test.A getter which will increment the
value of _Default.a static variable.
5. Continue the execution, and you will see "A = 1" on the page.
6. Close the browser. Start the page now not in debug mode. It will still
display "A = 1".
7. Try to restart IIS and run the page without debugger. Still "A = 1".
8. If you try to run it in a debugger and evaluate Test.A, it will display
"A = 2". Etc.
The only way to reset the value of a static variable _Default.a is to change
the page source code, so Visual Studio will detect the change and rebuild
the page. Then _Default.a will be reset to 0.
I understand that assembly is compiled once and cached. But it looks that
also the state of static variables is stored. And this looks dangerous, and
this is not how it worked in .NET 1.1. Is this behaviour by design?
Vagif Abilov
Oslo Norway
values of static variable instances. Actually it is reproduced with a little
piece of code.
I made a simlpe Web site consisting of an empty page. The page C# code file
looks like this:
using System;
using System.Web;
namespace StaticMemberTest
{
public class Test
{
static int A
{
get { _Default.a++; return _Default.a; }
}
}
public partial class _Default : System.Web.UI.Page
{
internal static int a = 0;
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("A = " + a.ToString());
}
}
}
Steps to reproduce the problem:
1. Set the breakpoint within Page_Load
2. Start the debugger.
3. Once the breakpoint is reached, go to Watch window and type "Test.A".
4. This will trigger evaluation of Test.A getter which will increment the
value of _Default.a static variable.
5. Continue the execution, and you will see "A = 1" on the page.
6. Close the browser. Start the page now not in debug mode. It will still
display "A = 1".
7. Try to restart IIS and run the page without debugger. Still "A = 1".
8. If you try to run it in a debugger and evaluate Test.A, it will display
"A = 2". Etc.
The only way to reset the value of a static variable _Default.a is to change
the page source code, so Visual Studio will detect the change and rebuild
the page. Then _Default.a will be reset to 0.
I understand that assembly is compiled once and cached. But it looks that
also the state of static variables is stored. And this looks dangerous, and
this is not how it worked in .NET 1.1. Is this behaviour by design?
Vagif Abilov
Oslo Norway