This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
!Object ()~Object();abstract member Finalize : unit -> unitoverride this.Finalize : unit -> unitFinalize ()The following example verifies that theFinalize method is called when an object that overridesFinalize is destroyed. Note that, in a production application, theFinalize method would be overridden to release unmanaged resources held by the object. Also note that the C# example provides a destructor instead of overriding theFinalize method.
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.0036294open System.Diagnosticstype 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.0036294Imports System.DiagnosticsPublic 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 SubEnd ClassModule Demo Public Sub Main() Dim ex As New ExampleClass() ex.ShowDuration() End SubEnd 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.0036294For an additional example that overrides theFinalize method, see theGC.SuppressFinalize method.
For more information about this API, seeSupplemental API remarks for Object.Finalize.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?