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 1114141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:57:51+00:00 2026-05-17T02:57:51+00:00

I am attempting to make a simple class that serializes itself to disk when

  • 0

I am attempting to make a simple class that serializes itself to disk when it is no longer in use. The code I have right now (see below). The code I have now seems to work, but I am not fully confident in my knowledge, so I am wondering if anyone else sees any significant problems with this code.

void IDisposable.Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}

~MyClass()
{
    Dispose(false);
}

protected virtual void Dispose(bool disposing)
{
    if (!this.disposed)
    {
        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(ms, this);
        byte[] output = Dostuff(ms);
        File.WriteAllBytes(DBPATH, output);
    }
    this.disposed = true;
}
  • 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-17T02:57:52+00:00Added an answer on May 17, 2026 at 2:57 am

    It is virtually impossible to write a finalizer correctly, and doing this sort of work in one is just a recipe for disaster. Not to mention it’ll kill performance and be impossible to debug. Rule 1 for finalizers is don’t ever use them. Rule 2 (for advanced users only) is don’t use them unless you’re really sure you have to.

    If it’s just for a fun hobby project then there’s no real harm and it will probably work well enough, but I’d cry if I ever saw something like this in a production codebase.

    If you do want to do something like this, then I’d make it an explicit call and simply use the finalizer to catch cases during debugging where the explicit method was not called, e.g.

    class MyClass
    {
        private bool dirty; // set this whenever the object changes
    
        ~MyClass 
        {
            if (this.dirty) 
            {
                Debug.Fail("Object was not saved.");
            }
        }
    
        public void Save()
        {
            if (this.dirty)
            {
                // TODO: do the save
                this.dirty = false;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am attempting to make a really simple lightbox that I can use
I'm attempting to make a simple linear text game that will display inside a
I'm attempting to make a simple homebrew game engine/framework in android. I have the
I am attempting to make a simple PhoneGap app that will allow a user
If I have a Customer class that just has simple properties (e.g. Name ,
I am attempting to make a simple accordion where the user clicks on level
Attempting to make a NSObject called 'Person' that will hold the login details for
I'm attempting to make use of the UIDocumentInteractionController mechanism in iPhone OS 3.2, but
I'm attempting to build a very simple wxPython GUI that monitors and displays external
I'm attempting to make a simple timer from 15 minutes to 0 seconds. I'm

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.