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

  • Home
  • SEARCH
  • 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 461369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:58:05+00:00 2026-05-12T22:58:05+00:00

I was making an RAII class that takes in a System.Windows.Form control, and sets

  • 0

I was making an RAII class that takes in a System.Windows.Form control, and sets its cursor. And in the destructor it sets the cursor back to what it was.

But is this a bad idea? Can I safely rely that the destructor will be called when objects of this class go out of scope?

  • 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-12T22:58:05+00:00Added an answer on May 12, 2026 at 10:58 pm

    This is a very, very bad idea.

    Finalizers are not called when variables go out of scope. They’re called at some point before the object is garbage collected, which could be a long time later.

    Instead, you want to implement IDisposable, and then callers can use:

    using (YourClass yc = new YourClass())
    {
        // Use yc in here
    }
    

    That will call Dispose automatically.

    Finalizers are very rarely necessary in C# – they’re only required when you directly own an unmanaged resource (e.g. a Windows handle). Otherwise, you typically have some managed wrapper class (e.g. FileStream) which will have a finalizer if it needs one.

    Note that you only need any of this when you have resources to be cleaned up – most classes in .NET don’t implement IDisposable.

    EDIT: just to respond to the comment about nesting, I agree it can be a bit ugly, but you shouldn’t need using statements very often in my experience. You can also nest usings vertically like this, in the case where you’ve got two directly adjacent ones:

    using (TextReader reader = File.OpenText("file1.txt"))
    using (TextWriter writer = File.CreateText("file2.txt"))
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Making a new class in VS10 gives me using System; using System.Collections.Generic; using System.Linq;
Im making a PyQt4 app which calls Imagemagick and ffmpeg, but it takes too
Im making a system which should be running 24/7, with timers to control it.
Making an adobe flex ui in which data that is calculated must use proprietary
Making UML sequence diagram in VS 2010RC I've observed that there is no activation
Making a flash page that can cycle through these three images on mouseclick. For
Making an Flex App. Just wondering if anyone has created something that fits automatically
Im making a collision checking system with jQuery however it doesn't seem to work.
Does making a class field volatile prevent all memory visibility issues with it in
Making a small WCF test program which is based on a Store that has

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.