Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:32:21+00:00 2026-05-24T04:32:21+00:00

I have a c# class. Whenever this class is not in use anymore I

  • 0

I have a c# class. Whenever this class is not in use anymore I want to do some things. For example log the current state and so on.

I want to be sure that this method is run everytime when the class is not used anymore.
I don’t want just use a simple method because I can’t be sure that every user is calling it.

I have no resources (like file handles) to clear up.

Is the best way to use a destructor?

“not in use” is when (for example):

  • a user uses my class in a form and the form is closed
  • the class is used in an application and this application is shut down
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:32:22+00:00Added an answer on May 24, 2026 at 4:32 am

    It depends. C# .NET utilizes a garbage collector that implicitly cleans up objects for you. Normally, you cannot control the clean up of objects – the garbage collector does that. You can implement a destructor in your class if you desire, but you may get a performance hit. MSDN has this to say on destructors:

    In general, C# does not require as much memory management as is needed
    when you develop with a language that does not target a runtime with
    garbage collection. This is because the .NET Framework garbage
    collector implicitly manages the allocation and release of memory for
    your objects. However, when your application encapsulates unmanaged
    resources such as windows, files, and network connections, you should
    use destructors to free those resources. When the object is eligible
    for destruction, the garbage collector runs the Finalize method of the
    object.

    and finally on performance:

    When a class contains a destructor, an entry is created in the
    Finalize queue. When the destructor is called, the garbage collector
    is invoked to process the queue. If the destructor is empty, this just
    causes a needless loss of performance.

    There are other ways to manage resources besides a destructor:

    Cleaning Up Unmanaged Resources

    Implementing a Dispose Method

    using Statement (C# Reference)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have a Question: Should I use databinding whenever I can? I will explain
I have a Date class which I would like to use to overwrite Ruby's
So suppose I have a tree class like this in c++ class Node{ void
I have a webapplication where I use a registry class. The registry class holds
I have some code in a reusable class that modifies some types. Here's a
I have found this example on StackOverflow: var people = new List<Person> { new
Whenever I have needed to create a class file from an XML XSD schema,
I have a sound cue that I want played whenever the player does a
I have a maven library project with some classes to deal xml messages. Whenever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.