ASP.NET 2.0:引用类型的静态实例并不总是设置为null [英] ASP.NET 2.0: static instance of reference type is not always set to null
问题描述
我们有几个Web项目,我们在将它们移植到.NET 2.0后突然开始遇到各种各样的问题。我调试了其中一个,这里是'
我发现了什么:
在一个引用的程序集中有一个类具有静态成员
其他课程:
A级
{
...
静态B b;
void Init()
{
if(b!= null)
{
b =新B();
}
}
}
问题与b有关。实例,所以我运行了一个调试器会话,并且发现我从未输入代码b = new B()。
。当我在调试会话中检查其值时,成员变量b
一直被实例化!
然后我稍微修改了代码:
A级
{
...
静态B b = null;
void Init()
{
if(b!= null)
{
b = new B();
}
}
}
然后一切正常。但为什么?如果我们有一个引用类型,为什么如果我们将这种类型的变量实例化为null或
不是,那么
会有什么不同?它不应该!引用类型实例必须设置为null。
我也尝试在非Web项目中运行此代码,但它在没有任何问题的情况下工作了
。它必须是与ASP.NET相关的东西。
有关此问题的任何信息表示赞赏。
Vagif Abilov
奥斯陆挪威
We have several Web projects where we suddenly began experiencing various
problems after porting them to .NET 2.0. I debugged one of them and here''s
what I''ve found:
In one of referenced assemblies there was a class with static members of
other class:
class A
{
...
static B b;
void Init()
{
if(b != null)
{
b = new B();
}
}
}
The problem was related to "b" instance, so I ran a debugger session and
discovered that I never entered code "b = new B()". The member variable "b"
has always been instantiated when I checked its value in debug session!
Then I slightly modified the code:
class A
{
...
static B b = null;
void Init()
{
if(b != null)
{
b = new B();
}
}
}
Then everything worked. But why? If we have a reference type, why does it
make any difference if we instantiate the variable of this type to null or
not? It shouldn''t! Reference types instances must be set to null.
I also tried to run this code in non-Web project, but there it worked
without any problem. It has to be something related to ASP.NET.
Any info on this issue is appreciated.
Vagif Abilov
Oslo Norway
推荐答案
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_csref/html/ 235614b5-1371-4dbd-9abd-b406a8b0298b.htm
:
------------------ -------------------------------------------------- -------------------------------------------------- ----
静态成员在第一次访问静态成员之前初始化,并且在静态构造函数之前(如果有的话)被调用。
---------------------------------------------- -------------------------------------------------- --------------------------
因此,假设成员b仅在Init中初始化()
通话,我们有:
b = null在Init调用之前。
这就是说,在Init()中有一些奇怪的东西例程:
我会说:
if(b == null)
{
b = new B();
}
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_csref/html/235614b5-1371-4dbd-9abd-b406a8b0298b.htm
:
--------------------------------------------------------------------------------------------------------------------------
Static members are initialized before the static member is accessed for
the first time, and before the static constructor, if any is called.
--------------------------------------------------------------------------------------------------------------------------
Therefore, supposing that member b is initialized only in the Init()
call, we have :
b=null before Init called.
This said, there''s something strange in Init() routine :
I would say :
if (b==null)
{
b=new B();
}
当然我拼错了Init例程 - 在实际代码中检查是否相等
null,as你写的。
关于你的报价:
静态成员在第一次访问
之前初始化静态成员,在静态构造函数之前,如果有任何调用。
最大的问题是:它们被初始化了什么。据我所知,所有
未分配的引用类型都被初始化为null。当我在调试器中运行此代码
时,我看到除非我明确地将它们分配给null,否则它们是分配给默认构造函数的
。并且只有在Web
项目中加载程序集时。我想知道为什么。
Vagif
" olrt" <醇** @ ifrance.com>在留言中写道
news:11 ********************** @ u72g2000cwu.googlegr oups.com ...
Of course I misspelled Init routine - in real code it checks for equality to
null, as you wrote.
Regarding your quote:
Static members are initialized before the static member is accessed for
the first time, and before the static constructor, if any is called.
The big question: to WHAT they are initialized. I understand that all
unassigned reference types are initialized to null. And when I run this code
in debugger, I see that unless I explicitly assign them to null, they are
assigned to default constructor. And only when assembly is loaded in Web
project. I wonder why.
Vagif
"olrt" <ol**@ifrance.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_csref/html/235614b5-1371-4dbd-9abd-b406a8b0298b.htm
:
------------------------------------------------ -------------------------------------------------- ------------------------
静态成员在第一次访问静态成员之前和之前初始化静态构造函数,如果有的话被调用。
-------------------------------------- -------------------------------------------------- ----------------------------------
因此,假设成员b已初始化只有在Init()
调用中,我们才有:
在Init调用之前b = null。
这就是说,在Init()例程中有一些奇怪的东西:
我会说:
if(b == null)
{
b = new B();
}
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_csref/html/235614b5-1371-4dbd-9abd-b406a8b0298b.htm
:
--------------------------------------------------------------------------------------------------------------------------
Static members are initialized before the static member is accessed for
the first time, and before the static constructor, if any is called.
--------------------------------------------------------------------------------------------------------------------------
Therefore, supposing that member b is initialized only in the Init()
call, we have :
b=null before Init called.
This said, there''s something strange in Init() routine :
I would say :
if (b==null)
{
b=new B();
}
>
我写了一个简单的用你的A班和B班考试申请。
我宣布
静态B b
而不是
静态B b = null;
当我测试应用程序时,我在Init()之前有b = null。
所以静态变量初始化为默认为null ...
问候。
I wrote a simple test application with your classes A and B.
I declared
static B b
instead of
static B b=null;
When I test the application, I have b=null before Init().
So static variables are initialized to null by default...
Regards.
这篇关于ASP.NET 2.0:引用类型的静态实例并不总是设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!