为什么在编译器中没有实现Dispose()? [英] Why isn't Dispose() implemented in the compiler?

查看:84
本文介绍了为什么在编译器中没有实现Dispose()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇是否有人知道为什么C#和VB.NET编译器不会?
在支持IDisposable的对象上自动调用Dispose()

超出范围。


我问了一位同事,他的回答是IDisposable是一个库

功能而不是语言功能。我指出using()

语句调用Dispose()而foreach使用IEnumerator。


那么为什么要留给开发人员搞砸了编译器可以

可以为你处理它吗?


有没有你打电话给Dispose()而不想让

对象超出范围?我不能想到一个单一的情况。还有一些

背后的其他推理吗?


Pete

I''m curious if anyone knows why the C# and VB.NET compilers don''t
automatically call Dispose() on objects that support IDisposable when they
go out of scope.

I asked a co-worker and his response was that IDisposable is a library
feature and not a language feature. To which I pointed out that the using()
statement calls Dispose() and foreach uses IEnumerator.

So why leave it to developers to screw up when the compiler could
conceivable take care of it for you?

Are there situations when you''d call Dispose() but not want to let the
object go out of scope? I can''t think of a single situation. Is there some
other reasoning behind it?

Pete

推荐答案

超出范围是我的朋友的一个难题。您可以将某个对象关闭

,其中包含一个引用,或者您可以从保存引用的函数

中获取它。在极少数情况下,您可以真实地分析

对象是否已超出范围。 JITer / GC组合有更好的机会,因为它有参考信息和范围

信息。

这就是为什么编译器人员将它留给平台确定

对象何时超出范围,语言处理对象引用。

-

Justin Rogers

DigiTec Web Consultants,LLC。

博客: http://weblogs.asp.net/justin_rogers

< pd ****** @ hotmail.com>在消息中写道

新闻:20 ****************************** @ news.meganet新闻。 com ...
Going out of scope is a hard call my friend. You could pass the object off
somewhere, that holds a reference, or you could get it back from a function
that holds a reference. There are very few cases where you can truly profile
whether an object has gone out of scope. The JITer/GC combo has a better
chance of this because it has reference information as well as scope
information.
That is why the compiler guys leave it to the platform to determine when an
object is out of scope, and languages deal with object references.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
<pd******@hotmail.com> wrote in message
news:20******************************@news.meganet news.com...
我很好奇,如果有人知道为什么C#和VB.NET编译器不会自动调用Dispose()来支持IDisposable的对象超出范围。

我问了一位同事,他的回答是IDisposable是一个库
功能而不是语言功能。我指出using()
语句调用Dispose()并且foreach使用IEnumerator。

那么为什么在编译器可以想象的时候让开发人员搞砸了呢?为你照顾它吗?

有没有你打电话给Dispose()而不想让
对象超出范围的情况?我不能想到一个单一的情况。背后是否还有一些其他推理?

Pete
I''m curious if anyone knows why the C# and VB.NET compilers don''t
automatically call Dispose() on objects that support IDisposable when they
go out of scope.

I asked a co-worker and his response was that IDisposable is a library
feature and not a language feature. To which I pointed out that the using()
statement calls Dispose() and foreach uses IEnumerator.

So why leave it to developers to screw up when the compiler could
conceivable take care of it for you?

Are there situations when you''d call Dispose() but not want to let the
object go out of scope? I can''t think of a single situation. Is there some
other reasoning behind it?

Pete



只是为了澄清,通过范围界定规则,我的意思是变量范围,而不是对象范围,我称之为对象生命周期。


Daniel Jin写道:
just to clarify, by scoping rules, I mean variable scopes, not object scopes, which I referred to as object lifetime.

"Daniel Jin" wrote:
因为编译器只能解析简单的作用域规则。编译器无法跟踪对象的生命周期,这是运行时的工作。并且运行时已经有功能确保对象得到正确清理(终结器)。

pd ****** @ hotmail.com写道:
because compiler can only resolve simple scoping rules. there''s no way a compiler can keep track an object''s lifetime, that''s the runtime''s job. and there is already feature in the runtime to make sure objects get cleaned up correctly (Finalizers).

"pd******@hotmail.com" wrote:
我很好奇,如果有人知道为什么C#和VB.NET编译器不会在支持IDisposable的对象上自动调用Dispose()
超出范围。

我问了一位同事,他的回答是IDisposable是一个库
功能而不是语言功能。我指出using()
语句调用Dispose()并且foreach使用IEnumerator。

那么为什么在编译器可以想象的时候让开发人员搞砸了呢?为你照顾它吗?

有没有你打电话给Dispose()而不想让
对象超出范围的情况?我不能想到一个单一的情况。背后有其他推理吗?

Pete
I''m curious if anyone knows why the C# and VB.NET compilers don''t
automatically call Dispose() on objects that support IDisposable when they
go out of scope.

I asked a co-worker and his response was that IDisposable is a library
feature and not a language feature. To which I pointed out that the using()
statement calls Dispose() and foreach uses IEnumerator.

So why leave it to developers to screw up when the compiler could
conceivable take care of it for you?

Are there situations when you''d call Dispose() but not want to let the
object go out of scope? I can''t think of a single situation. Is there some
other reasoning behind it?

Pete



您好pdavis68 ,


你有答案。编译器会为你发出对Dispose的调用,但当你使用* using *运算符并且还有一些其他情况

我可以想到此时正处于foreach循环中它调用

处理枚举器实现IDisposable


否则对编译器知道何时对象来说是不可能的

超出范围。编译器可以轻松地说出值类型何时超出范围,

但不一定是值类型的一次性。对于参考类型而言,它不是简单的因为它们可以在代码中的许多地方引用。所以

只有一个确定的事情是当GC收集对象时。如果一个对象有一个

终结器,那么很可能它也是一次性的,终结器调用

dispose方法。然而,这又是程序员的责任,

编译器无法做更多关于它的事情,因为它无法知道如何实现

dispose方法以及它是否安全调用它(对象可以

已经处理完毕)。


编译器不能覆盖生成的代码,否则可能会生成

在某些情况下不可能使用的语言。有些语言带有

更高级别的抽象(主要是脚本语言)可以做任何他们想要的东西并生成大量代码,但这些语言专门用于某些

学位和他们的应用程序域非常小。

C#不是那种语言。它面向广泛的应用程序

(几乎所有.NET平台应用程序都可以用C#编写)


-

HTH

Stoitcho Goutsev(100)[C#MVP]

< pd ****** @ hotmail.com>在消息中写道

新闻:20 ****************************** @ news.meganet新闻。 com ...
Hi pdavis68,

You have the answer. The compiler emits calls to Dispose for you, but when
it is safe- when you use *using* operator and there are some other cases the
one I can think of at the moment is at the and of foreach loop it calls
Dispose of the enumerator implements IDisposable

Otherwise is neraly imposible for the compiler to know when an object goes
out of scope. Compiler could easely say when a value type goes out of scope,
but is not likely valuetypes to be disposable. For reference types it is not
that easy because they can be referenced in many places in the code. So the
only one sure thing is that when the GC collects objects. If an object has a
finalizer it is most likely it is disposable as well and the finalizer calls
the dispose method. However this is again resposibilty of the programmer the
compilier cannot do more about it because it has no way to know how the
dispose method is implemented and if it is safe to call it (the object can
be already disposed).

The compiler cannot overdo with the code generated otherwise it could make
the language impossible to use in some scenarios. There some languages with
higher level of abstraction (mostly scrip languages) can do what ever they
want and generate a lot of code, but those languages are specialized in some
degree and their application domain is pretty small.
C# is not that kind of language. It targets wide spectrum of application
(almost any application for .NET platform can be written in C#)

--
HTH
Stoitcho Goutsev (100) [C# MVP]
<pd******@hotmail.com> wrote in message
news:20******************************@news.meganet news.com...
我很好奇,如果有人知道为什么C#和VB.NET编译器不会自动调用Dispose()来支持IDisposable的对象超出范围。

我问了一位同事,他的回答是IDisposable是一个库
功能而不是语言功能。我指出
using()语句调用Dispose()而foreach使用IEnumerator。

那么为什么要让开发人员在编译器可以想象时搞砸了呢?为你照顾它吗?

有没有你打电话给Dispose()而不想让
对象超出范围的情况?我不能想到一个单一的情况。背后有其他推理吗?

Pete
I''m curious if anyone knows why the C# and VB.NET compilers don''t
automatically call Dispose() on objects that support IDisposable when they
go out of scope.

I asked a co-worker and his response was that IDisposable is a library
feature and not a language feature. To which I pointed out that the using() statement calls Dispose() and foreach uses IEnumerator.

So why leave it to developers to screw up when the compiler could
conceivable take care of it for you?

Are there situations when you''d call Dispose() but not want to let the
object go out of scope? I can''t think of a single situation. Is there some
other reasoning behind it?

Pete



这篇关于为什么在编译器中没有实现Dispose()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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