Compartir a través de


Object.Finalize Método

Definición

Permite que un objeto intente liberar recursos y realizar otras operaciones de limpieza antes de que sea reclamado por la recolección de elementos no utilizados.

!Object ()
~Object ();
abstract member Finalize : unit -> unit
override this.Finalize : unit -> unit
Finalize ()

Ejemplos

En el ejemplo siguiente se comprueba que se llama al Finalize método cuando se destruye un objeto que invalida Finalize . Tenga en cuenta que, en una aplicación de producción, el Finalize método se reemplazaría para liberar recursos no administrados mantenidos por el objeto . Tenga en cuenta también que el ejemplo de C# proporciona un destructor en lugar de invalidar el Finalize método .

using System;
using System.Diagnostics;

public class ExampleClass
{
   Stopwatch sw;

   public ExampleClass()
   {
      sw = Stopwatch.StartNew();
      Console.WriteLine("Instantiated object");
   }

   public void ShowDuration()
   {
      Console.WriteLine("This instance of {0} has been in existence for {1}",
                        this, sw.Elapsed);
   }

   ~ExampleClass()
   {
      Console.WriteLine("Finalizing object");
      sw.Stop();
      Console.WriteLine("This instance of {0} has been in existence for {1}",
                        this, sw.Elapsed);
   }
}

public class Demo
{
   public static void Main()
   {
      ExampleClass ex = new ExampleClass();
      ex.ShowDuration();
   }
}
// The example displays output like the following:
//    Instantiated object
//    This instance of ExampleClass has been in existence for 00:00:00.0011060
//    Finalizing object
//    This instance of ExampleClass has been in existence for 00:00:00.0036294
open System.Diagnostics

type ExampleClass() =
    let sw = Stopwatch.StartNew()
    do 
        printfn "Instantiated object"

    member this.ShowDuration() =
        printfn $"This instance of {this} has been in existence for {sw.Elapsed}"

    override this.Finalize() =
        printfn "Finalizing object"
        sw.Stop()
        printfn $"This instance of {this} has been in existence for {sw.Elapsed}"

let ex = ExampleClass()
ex.ShowDuration()
// The example displays output like the following:
//    Instantiated object
//    This instance of ExampleClass has been in existence for 00:00:00.0011060
//    Finalizing object
//    This instance of ExampleClass has been in existence for 00:00:00.0036294
Imports System.Diagnostics

Public Class ExampleClass
   Dim sw As StopWatch
   
   Public Sub New()
      sw = Stopwatch.StartNew()
      Console.WriteLine("Instantiated object")
   End Sub 

   Public Sub ShowDuration()
      Console.WriteLine("This instance of {0} has been in existence for {1}",
                        Me, sw.Elapsed)
   End Sub
   
   Protected Overrides Sub Finalize()
      Console.WriteLine("Finalizing object")
      sw.Stop()
      Console.WriteLine("This instance of {0} has been in existence for {1}",
                        Me, sw.Elapsed)
   End Sub
End Class

Module Demo
   Public Sub Main()
      Dim ex As New ExampleClass()
      ex.ShowDuration()
   End Sub
End Module
' The example displays output like the following:
'    Instantiated object
'    This instance of ExampleClass has been in existence for 00:00:00.0011060
'    Finalizing object
'    This instance of ExampleClass has been in existence for 00:00:00.0036294

Para obtener un ejemplo adicional que invalida el Finalize método , consulte el GC.SuppressFinalize método .

Comentarios

Para obtener más información sobre esta API, consulte Comentarios complementarios de la API para Object.Finalize.

Se aplica a

Consulte también