Object.Finalize Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Tillåter att ett objekt försöker frigöra resurser och utföra andra rensningsåtgärder innan det frigörs av skräpinsamling.
!Object ()
~Object();
abstract member Finalize : unit -> unit
override this.Finalize : unit -> unit
Finalize ()
Exempel
I följande exempel verifieras att Finalize metoden anropas när ett objekt som åsidosätter förstörs Finalize . Observera att metoden i ett produktionsprogram Finalize åsidosätts för att frigöra ohanterade resurser som innehas av objektet. Observera också att C#-exemplet ger en destruktor i stället för att Finalize åsidosätta metoden.
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
Ett annat exempel som åsidosätter Finalize metoden finns i GC.SuppressFinalize metoden.
Kommentarer
Mer information om det här API:et finns i Kompletterande API-kommentarer för Object.Finalize.