Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Object.Finalize Method

Definition

Namespace:
System
Assemblies:
mscorlib.dll, System.Runtime.dll
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.Runtime.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll
Source:
Object.cs
Source:
Object.cs
Source:
Object.cs
Source:
Object.cs

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 -> unit
Finalize ()

Examples

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.0036294
open 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.0036294
Imports 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.0036294

For an additional example that overrides theFinalize method, see theGC.SuppressFinalize method.

Remarks

For more information about this API, seeSupplemental API remarks for Object.Finalize.

Applies to

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?