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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:09:17+00:00 2026-05-25T12:09:17+00:00

This is a fairly fine point, and I expect the answer is it’s not

  • 0

This is a fairly fine point, and I expect the answer is “it’s not a hot idea to begin with” – that said, it has a points that I’m interested in regardless, if someone is kind enough to indulge.

Model Code:

public partial class MyEntities : ObjectContext
{
    // the idea is if the object is in a using block, this always gets called?
    protected override void Dispose(bool disposing)
    {
        this.SaveChanges();
        base.Dispose(disposing);
    }
}

Client Code:

using(var model = new MyEntities())
{
   // do something

   // no worry about calling model.SaveChanges()
}

The issues I’m uncertain about are:

  1. Is Dispose the right place to do this because I was thinking “Finalize” for some reason – I always get confused on C# destruction.

  2. In the case an exception is thrown in the client code, ordinarily SaveChanges would be skipped and that’s good, but if this works how I think, it’ll always call it. Should I use try with an empty catch?

    public partial class MyEntities : ObjectContext
    {
        protected override void Dispose(bool disposing)
        {
            try
            {
               this.SaveChanges();
            }
            catch {}
            base.Dispose(disposing);
        }
    }
    
  • 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-25T12:09:17+00:00Added an answer on May 25, 2026 at 12:09 pm

    Don’t do this. It’s a bad idea.

    The purpose of “Dispose” is to politely dispose of an unmanaged resource early so that other processes can use it. “Dispose” should not have semantics — it should not change the state of your program or be required in some way. It should only do precisely what it says it does: dispose of a resource.

    Should you do it in the finalizer? Absolutely not. That’s even worse. The finalizer might not run at all, the finalizer runs on another thread, the finalizer can be called even if the object wasn’t properly initialized, and so on. Writing a finalizer is almost never the right thing to do, and if you do write a finalizer it should only dispose of a resource. Do not do anything fancy in a finalizer; you will almost certainly write a dangerously incorrect and brittle program if you do.

    The right principle to cleave to here is: if a call is required for semantic reasons then force the user to put the call in the code. If they forget to do it, they’ll find out in testing. Let the user decide whether it is the right thing to do to put the call in a finally block or not. Don’t make that decision for them; you might decide wrong.

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

Sidebar

Related Questions

This is a fairly common problem, it probably has a name, I just don't
I have an HTML (not XHTML) document that renders fine in Firefox 3 and
I'm currently working on a fairly large project that has been migrated from Ant
I have a file that has fairly long lines. The longest line has length
I'm trying to use this fairly standard line of code in my app: [[UIApplication
Say I am storing addresses in a DB table, in this fairly common break
I know this is fairly subjective, but I'm diving into testing and learning about
I guess this is fairly simple for you but i cant wrap my head
I'm sure this is fairly simple, however I have a major mental block on
I'd like to count and group rows by specific values. This seems fairly simple,

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.