C#和析构函数 [英] C# and destructors

查看:89
本文介绍了C#和析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




让我说我有课堂(C#)会创造某种形式的沟通

chanel,这是非常昂贵的因此,当不再需要时,应该关闭计算机资源和

。我有一个方法,

将创建这个chanel和一个方法,它将关闭/释放它。


我的问题是:有没有我可以的地方如果用户

不调用我的发布方法,请释放此chanel?我可以写这样的东西:


公共类MyClass {

//我的沟通香奈儿

私人TheChannel m_Channel;


public MyClass(){

m_Channel = null;

}


~MyClass (){

if(m_Channel!= null){

//发布频道

}

}


public void CreateChannel(){// ...}

public void CloseChannel(){// ...}

}


此方法似乎有效!但文档说C#没有

析构函数。它还说应该使用Dispose-method,就像所有的

..NET类一样,但它不起作用!

Kimmo Laine

Hi,

lets say that i have class (C#) which will create some kind of communication
chanel, which is very expensive in terms of computer resources, and
therefore should be closed, when no longer needed. I have a method which
will create this chanel and a method which will close/release it.

My question is: is there a place where i can release this chanel if user
don′t call my release method? Can i write something like this:

public class MyClass {
// My communication chanel
private TheChannel m_Channel;

public MyClass() {
m_Channel = null;
}

~MyClass() {
if( m_Channel != null ) {
// Release channel
}
}

public void CreateChannel() { //... }
public void CloseChannel() { //... }
}

This method seems to work! But documentation says that "C# has no
destructor." It also says that is should use Dispose-method, like all the
..NET classes do, but it doesn''t work!
Kimmo Laine

推荐答案

您好,


请参阅实施IDisposable接口的MSDN库

组件和相关的设计模式。


-

Dmitriy Lapshin [C#/ .NET MVP]

X-Unity测试工作室
http://www.x-unity.net/ teststudio.aspx

将单元测试的强大功能带到VS .NET IDE


" Kimmo Laine" < reply.to@newsgroup>在消息中写道

news:uk ************** @ TK2MSFTNGP09.phx.gbl ...
Hi,

Please refer to the MSDN library on implementing the IDisposable interface
in components and the related design pattern.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...

让我说我有一个类(C#)会创建某种
通信chanel,这在计算机资源方面非常昂贵,因此应该关闭,不再需要。我有一个方法可以创建这个chanel和一个关闭/释放它的方法。

我的问题是:如果用户
不要调用我的发布方法?我可以写这样的东西:

公共课MyClass {
//我的沟通chanel
私人TheChannel m_Channel;

公共MyClass(){
m_Channel = null;
}
~MyClass(){
if(m_Channel!= null){
//发布频道
}
}

public void CreateChannel(){// ...}
public void CloseChannel(){// ...}
}

这种方法似乎有效!但是文档说C#没有
析构函数。它还说应该像所有的.NET类一样使用Dispose-method,但它不起作用!

Kimmo Laine
Hi,

lets say that i have class (C#) which will create some kind of communication chanel, which is very expensive in terms of computer resources, and
therefore should be closed, when no longer needed. I have a method which
will create this chanel and a method which will close/release it.

My question is: is there a place where i can release this chanel if user
don′t call my release method? Can i write something like this:

public class MyClass {
// My communication chanel
private TheChannel m_Channel;

public MyClass() {
m_Channel = null;
}

~MyClass() {
if( m_Channel != null ) {
// Release channel
}
}

public void CreateChannel() { //... }
public void CloseChannel() { //... }
}

This method seems to work! But documentation says that "C# has no
destructor." It also says that is should use Dispose-method, like all the
.NET classes do, but it doesn''t work!
Kimmo Laine






不太正确(不确定你在哪里阅读)。 C#有一个构造函数和

destructer,你已经在代码中实现了后者。

有些类已经继承并实现了IDisposible接口。

否则该类必须继承它以允许您使用和

实现Dispose方法。所以在你的代码中你需要...

公共类MyClass:IDisposible

{..

Dispose()

{

//在这里实施(清理)

..

}

}


-


-


Br,

Mark Broadbent

mcdba,mcse + i

=============

" Kimmo Laine" < reply.to@newsgroup>在消息中写道

news:uk ************** @ TK2MSFTNGP09.phx.gbl ...
not quite true (not sure where you read that). C# has both a constructor and
destructer, and you have implemented the latter in your code.
Some classes already inherit and implement the IDisposible interface.
Otherwise the class must inherit it to allow you to make available and
implement the Dispose method. So in your code you would need...
public class MyClass: IDisposible
{..
Dispose()
{
//implementation here (clean up)
..
}
}

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...

让我说我有一个类(C#)会创建某种
通信chanel,这在计算机资源方面非常昂贵,因此应该关闭,不再需要。我有一个方法可以创建这个chanel和一个关闭/释放它的方法。

我的问题是:如果用户
不要调用我的发布方法?我可以写这样的东西:

公共课MyClass {
//我的沟通chanel
私人TheChannel m_Channel;

公共MyClass(){
m_Channel = null;
}
~MyClass(){
if(m_Channel!= null){
//发布频道
}
}

public void CreateChannel(){// ...}
public void CloseChannel(){// ...}
}

这种方法似乎有效!但是文档说C#没有
析构函数。它还说应该像所有的.NET类一样使用Dispose-method,但它不起作用!

Kimmo Laine
Hi,

lets say that i have class (C#) which will create some kind of communication chanel, which is very expensive in terms of computer resources, and
therefore should be closed, when no longer needed. I have a method which
will create this chanel and a method which will close/release it.

My question is: is there a place where i can release this chanel if user
don′t call my release method? Can i write something like this:

public class MyClass {
// My communication chanel
private TheChannel m_Channel;

public MyClass() {
m_Channel = null;
}

~MyClass() {
if( m_Channel != null ) {
// Release channel
}
}

public void CreateChannel() { //... }
public void CloseChannel() { //... }
}

This method seems to work! But documentation says that "C# has no
destructor." It also says that is should use Dispose-method, like all the
.NET classes do, but it doesn''t work!
Kimmo Laine



Dmitriy Lapshin [C#/ .NET MVP]< x - **** @ no-spam-please.hotpop.com>

写道:
Dmitriy Lapshin [C# / .NET MVP] <x-****@no-spam-please.hotpop.com>
wrote:
请参阅MSDN库,了解如何在组件和相关设计模式中实现IDisposable接口。
Please refer to the MSDN library on implementing the IDisposable interface
in components and the related design pattern.




at:

http://tinyurl.com/2k6e


请注意,你的MyClass类可能*不应该*有一个终结器 -

只是一个Dispose方法。你的TheChannel课程可以有一个终结者

虽然。


我正在接受这样的想法,实际上,所有终结者应该是

类似于:


#if DEBUG

~Foo

{

系统。 Windows.Forms.MessageBox.Show(" Dispose not called on Foo");

}

#endif


终结者不应该最终被使用 - 如果一个人被调用,那么

通常意味着程序中有一个错误(假设Dispose

正确地抑制了最终确定)和如果你没有解决它,这个bug可能会咬你(在
难以察觉的时间)。因此,不要假装这个bug不存在(通过尝试在

终结器中修补),而不是假装

,引起开发人员注意事实上,他首先得到了

的错误。


这是一种积极的方法,但确实有吸引力...... 。


-

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

如果回复该群组,请不要给我发邮件



Namely at:

http://tinyurl.com/2k6e

Note that your MyClass class probably *shouldn''t* have a finalizer -
just a Dispose method. Your TheChannel class could have a finalizer
though.

I''m coming round to the idea that actually, all finalizers should be
something like:

#if DEBUG
~Foo
{
System.Windows.Forms.MessageBox.Show ("Dispose not called on Foo");
}
#endif

Finalizers shouldn''t end up being used - if one gets called, that
usually means there''s a bug in the program (assuming that Dispose
correctly suppresses finalization) and that bug may well bite you (at a
hard-to-detect time) if you don''t fix it. So, instead of pretending
that the bug doesn''t exist (by attempting to patch things up in the
finalizer), draw a developer''s attention to the fact that he''s got the
bug in the first place.

It''s a bit of an aggressive approach, but it does appeal...

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


这篇关于C#和析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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