处置然后设置为空 [英] Dispose then set to nothing

查看:70
本文介绍了处置然后设置为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在许多MS示例代码中看到这样的内容


Public Sub Foo()

dim myCustomer作为新客户

...

myCustomer.Dispose()

myCustomer = Nothing

结束子


我想知道在这种情况下设置myCustomer = Nothing是否必要?

因为myCustomer是一个私有变量,一旦它熄灭在下一个

行的范围内,应该释放对象的引用。


谢谢。



Hi,

I see in many MS example code that looks like this

Public Sub Foo()
dim myCustomer as New Customer
...
myCustomer.Dispose()
myCustomer = Nothing
End Sub

I''m wondering if setting myCustomer = Nothing is necessary in this case?
Since myCustomer is a private variable, once it goes out of scope at the next
line, the reference to the object should be released.

Thanks.

Hao

推荐答案

Hao Jiang写道:
Hao Jiang wrote:




我看到许多MS示例代码看起来像这样

Public Sub Foo()

dim myCustomer作为新客户

...

myCustomer.Dispose()

myCustomer = Nothing

End Sub


我想知道是否设置myCustomer = Noth在这种情况下是必要的吗?

由于myCustomer是一个私有变量,一旦它超出下一个

行的范围,就应该释放对该对象的引用。


谢谢。


好​​
Hi,

I see in many MS example code that looks like this

Public Sub Foo()
dim myCustomer as New Customer
...
myCustomer.Dispose()
myCustomer = Nothing
End Sub

I''m wondering if setting myCustomer = Nothing is necessary in this case?
Since myCustomer is a private variable, once it goes out of scope at the next
line, the reference to the object should be released.

Thanks.

Hao



这是正确的。在这种情况下删除引用是没有意义的。


-

G ??运行Andersson

_____
< a rel =nofollowhref =http://www.guffa.comtarget =_ blank> http://www.guffa.com


" Hao Jiang" < Hao Ji***@discussions.microsoft.com schrieb:
"Hao Jiang" <Hao Ji***@discussions.microsoft.comschrieb:

Public Sub Foo()

dim myCustomer作为新客户

...

myCustomer.Dispose()

myCustomer = Nothing

End Sub


我想知道在这种情况下设置myCustomer = Nothing是否必要?
Public Sub Foo()
dim myCustomer as New Customer
...
myCustomer.Dispose()
myCustomer = Nothing
End Sub

I''m wondering if setting myCustomer = Nothing is necessary in this case?



不,你提到的原因实际上没有必要。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http:// dotnet.mvps.org/dotnet/faqs/>

No, it actually isn''t necessary for the reason you mentioned.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


郝江,

由于G ?? ran& Herfried建议设置myCustomer =你不需要什么就可以了。


而不是直接调用Dispose我建议使用新的使用
.NET 2.0中的
语句(VS 2005)。 Using语句确保Dispose被调用
,即使其中一个语句引发了

异常。
Hao Jiang,
As G??ran & Herfried suggests setting myCustomer = Nothing is not needed for
the reason you give.

Instead of calling Dispose outright I would recommend using the new Using
statement in .NET 2.0 (VS 2005). The Using statement ensures that Dispose is
called even if one of the statements contained by the Using throws an
exception.

Public Sub Foo()
Public Sub Foo()



使用myCustomer作为新客户

Using myCustomer as New Customer


...
...



结束使用

End Using


End Sub
End Sub



.NET 1.x中基本上是以下内容:


将我的客户称为新客户

尝试

DoSomething()

最后

如果myCustomer IsNot Nothing那么

myCustomer.Dispose()

结束如果

结束尝试


-

希望这有帮助

Jay B. Harlow [MVP - Outlook]

..NET应用程序架构师,爱好者,&福音传教士

T.S.布拉德利 - http://www.tsbradley.net

" ;郝江 < Hao Ji***@discussions.microsoft.com 在留言中写道

news:45 ********************************** @ microsof t.com ...

Which is basically the following in .NET 1.x:

Dim myCustomer As New Customer
Try
DoSomething()
Finally
If myCustomer IsNot Nothing Then
myCustomer.Dispose()
End If
End Try

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Hao Jiang" <Hao Ji***@discussions.microsoft.comwrote in message
news:45**********************************@microsof t.com...





我在许多MS示例代码中看到这样的内容


Public Sub Foo()

将myCustomer视为新客户

...

myCustomer.Dispose()

myCustomer = Nothing

结束子


我想知道在这种情况下是否需要设置myCustomer = Nothing?

因为myCustomer是一个私有变量一旦它超出了

下一个

行的范围,就应该释放对该对象的引用。


谢谢。


Hao
Hi,

I see in many MS example code that looks like this

Public Sub Foo()
dim myCustomer as New Customer
...
myCustomer.Dispose()
myCustomer = Nothing
End Sub

I''m wondering if setting myCustomer = Nothing is necessary in this case?
Since myCustomer is a private variable, once it goes out of scope at the
next
line, the reference to the object should be released.

Thanks.

Hao


这篇关于处置然后设置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆