[Mono-devel-list] About some object-drop actions (Finalizers and IDisposable)

Chris Masters neeeeeep at bigpond.com
Mon Dec 1 20:22:17 EST 2003


Would a C# using block acheive what you want? It calls dispose on the object
you specify upon exiting the using block. Or is this still too dependant on
the client for you?

using System;

public class App
{
     public static void Main()
     {
         using (SomeClass s = new SomeClass())
         {
              s.HelloWorld();
         }
     }
}

public class SomeClass : IDisposable
{
     public void HelloWorld()
     {
         Console.WriteLine("Hello World!");
     }
     public void Dispose()
     {
         Console.WriteLine("disposing!");
     }
}

GC.Collect would do it I suppose, but is bad to do explicitly in the .NET
framework as it promotes objects to the next gen if they aren't collectable,
but I guess thats not an issue in Mono since its not a generational GC atm.

Cheers,

Chris



----- Original Message ----- 
From: <yoros at wanadoo.es>
To: "mono-devel-list" <mono-devel-list at lists.ximian.com>
Sent: Tuesday, December 02, 2003 7:04 AM
Subject: [Mono-devel-list] About some object-drop actions (Finalizers and
IDisposable)


>
> Hi,
>
> I need to do some tasks befor a class is freed (at least when the
> application ends) but I don't want any client interaction (IDispose
> needs to explicit notice when a class is never used). I have a few
> singleton classes and I want to serialize internal objects but I don't
> want to depend of the clients of these classes. Is there any other way
> to ensure the Finalizer execution (with GC class or something...) ?
>
> Thank you,
>
>     Pedro
>
> -- 
> Pedro Martínez Juliá
> \  yoros at terra.es
> )|    yoros at wanadoo.es
> /        http://yoros.dyndns.org
> Socio HispaLinux #311
> Usuario Linux #275438 - http://counter.li.org
> GnuPG public information:  pub  1024D/74F1D3AC
> Key fingerprint = 8431 7B47 D2B4 5A46 5F8E  534F 588B E285 74F1 D3AC
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list




More information about the Mono-devel-list mailing list