Re: C# and destructors

From: Dmitriy Lapshin [C# / .NET MVP] (x-code_at_no-spam-please.hotpop.com)
Date: 04/23/04


Date: Fri, 23 Apr 2004 16:39:43 +0300

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:uktvoZTKEHA.3472@TK2MSFTNGP09.phx.gbl...
> 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
>
>


Relevant Pages

  • Re: C# and destructors
    ... destructer, and you have implemented the latter in your code. ... implement the Dispose method. ... public class MyClass: IDisposible ... > will create this chanel and a method which will close/release it. ...
    (microsoft.public.dotnet.languages.csharp)
  • C# and destructors
    ... lets say that i have class which will create some kind of communication ... will create this chanel and a method which will close/release it. ... public class MyClass { ...
    (microsoft.public.dotnet.languages.csharp)