处理与否? [英] Dispose or not??

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

问题描述

我曾经认为应该处理所有实现

IDisposable的对象。但是我发现80%的

类实现了IDisposable。


但是当我查看样本时,大多数对象都不是

处置。一般来说,他们是Gdi对象,表单控件,大多数组件等等。


为什么?我怎么决定处理?

解决方案




在大多数情况下,你不要不必明确地调用dispose方法。垃圾

收集器将处理所有无法访问的对象,而不需要接口界面。

C#GC管理它们很好,所以大多数这些示例,你已经提到了让

GC管理它。


Nirosh。


" qrli" <一个******* @ discussions.microsoft.com>在消息中写道

news:04 **************************** @ phx.gbl ... < blockquote class =post_quotes>我曾经认为应该处理所有实现
IDisposable的对象。但我发现80%的
类实现了IDisors。

但是当我查看样本时,大多数对象都没有被处理掉。通常,它们是Gdi对象,表单控件,大多数组件等。

为什么?我怎么决定处理?



除非一个对象的类要求你明确地调用Dispose,否则你没有/>
到。 GC会照顾。


大多数GDI对象都要求你调用Dispose - 在内部释放他们的句柄




vJ


" qrli" <一个******* @ discussions.microsoft.com>在消息中写道

news:04 **************************** @ phx.gbl ... < blockquote class =post_quotes>我曾经认为应该处理所有实现
IDisposable的对象。但我发现80%的
类实现了IDisors。

但是当我查看样本时,大多数对象都没有被处理掉。通常,它们是Gdi对象,表单控件,大多数组件等。

为什么?我怎么决定处理?



qrli< an ******* @ discussion.microsoft.com>写道:

我曾经认为应该处理所有实现
IDisposable的对象。但我发现80%的
类实现了IDisposable。


80%?你在哪里得到那个数字?这对我来说听起来非常高。

但是当我查看样品时,大部分物品都没有被处理掉。通常,它们是Gdi对象,表单控件,大多数组件等。


表单控件通常由
$创建的默认代码处理b $ b VS.NET。

为什么?我怎么决定处理?




我会鼓励你总是确保你处理的物品是

实施IDisposable。 br />

-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请执行不要给我发邮件


I used to think that all objects that implement
IDisposable should be disposed. But I found 80% of the
classes implement IDisposable.

But when I looked into the samples, most objects are not
disposed. Generally, they''re Gdi objects, form controls,
most components, etc.

So why? How can I decide to dispose or not?

解决方案

Hi,

In most cases you don''t have to call dispose method explicitly. The garbage
collector will dispose all unreachable object without puasing the interface.
C# GC manages them well, so most of these sample, you have refered have let
GC to mange it.

Nirosh.

"qrli" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...

I used to think that all objects that implement
IDisposable should be disposed. But I found 80% of the
classes implement IDisposable.

But when I looked into the samples, most objects are not
disposed. Generally, they''re Gdi objects, form controls,
most components, etc.

So why? How can I decide to dispose or not?



Unless an object''s class asks you to explicitly call Dispose, you don''t have
to. The GC will take care.

Most GDI objects require you to call Dispose - to release their handle
internally.

vJ

"qrli" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...

I used to think that all objects that implement
IDisposable should be disposed. But I found 80% of the
classes implement IDisposable.

But when I looked into the samples, most objects are not
disposed. Generally, they''re Gdi objects, form controls,
most components, etc.

So why? How can I decide to dispose or not?



qrli <an*******@discussions.microsoft.com> wrote:

I used to think that all objects that implement
IDisposable should be disposed. But I found 80% of the
classes implement IDisposable.
80%? Where do you get that figure? That sounds awfully high to me.
But when I looked into the samples, most objects are not
disposed. Generally, they''re Gdi objects, form controls,
most components, etc.
Form controls will usually be disposed by the default code created by
VS.NET.
So why? How can I decide to dispose or not?



I would encourage you to always make sure you dispose of objects which
implement IDisposable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于处理与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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