Class Cs.DestructorDeclaration

java.lang.Object
org.openrewrite.csharp.tree.Cs.DestructorDeclaration
All Implemented Interfaces:
Cs, org.openrewrite.java.tree.J, org.openrewrite.java.tree.Statement, org.openrewrite.Tree
Enclosing interface:
Cs

public static class Cs.DestructorDeclaration extends Object implements Cs, org.openrewrite.java.tree.Statement
Represents a C# destructor which is a method called before an object is destroyed by the garbage collector. A destructor must be named the same as the class prefixed with a tilde (~), cannot be explicitly called, cannot have parameters or access modifiers, and cannot be overloaded or inherited.

For example:

     // Basic destructor
     ~MyClass()
     {
         // Cleanup code
     }

     // Destructor with cleanup logic
     ~ResourceHandler()
     {
         if (handle != IntPtr.Zero)
         {
             CloseHandle(handle);
         }
     }

     // Class with both constructor and destructor
     public class FileWrapper
     {
         public FileWrapper()
         {
             // Initialize
         }

         ~FileWrapper()
         {
             // Cleanup
         }
     }
 

Note: In modern C#, it's recommended to implement IDisposable pattern instead of relying on destructors for deterministic cleanup of resources, as destructors are non-deterministic and can impact performance.

  • Constructor Details

    • DestructorDeclaration

      public DestructorDeclaration()
  • Method Details

    • acceptCSharp

      public <P> org.openrewrite.java.tree.J acceptCSharp(CSharpVisitor<P> v, P p)
      Specified by:
      acceptCSharp in interface Cs
    • getCoordinates

      public org.openrewrite.java.tree.CoordinateBuilder.Statement getCoordinates()
      Specified by:
      getCoordinates in interface org.openrewrite.java.tree.Statement