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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:24:40+00:00 2026-05-30T13:24:40+00:00

Suppose I have a class DisposableObject which implements IDisposable. There is a risk it

  • 0

Suppose I have a class DisposableObject which implements IDisposable. There is a risk it can throw an exception from any constructor and from the DoStuff function. I want to do something like this, so that no matter what happens, the object is disposed properly:

edit: I would like to avoid duplicate code, so a using inside each if/else block is not desirable (the code is a lot more complex than here)

edit2: Yes the try/finally error was wrong, changed it to use of unassigned local variable.

using (DisposableObject worker)
{
    if(/*condition*/)
        worker = new DisposableObject(/*args*/)
    else
        worker = new DisposableObject(/*other args*/)
    worker.DoStuff();
}

But I can’t, because the compiler says I must provide an initializer in the using statement.
I can’t use try/finally either:

DisposableObject worker;
try
{
    if(/*condition*/)
        worker = new DisposableObject(/*args*/)
    else
        worker = new DisposableObject(/*other args*/)
    worker.DoStuff();
}
finally
{
    worker.Dispose();
}

The compiler says use of unassigned local variable ‘worker’.

So what can I do? Obviously I can’t simply go like this…

DisposableObject worker;
if (/*condition*/)
    worker = new DisposableObject(/*args*/)
else
    worker = new DisposableObject(/*other args*/)
worker.DoStuff();
worker.Dispose();

Because I can’t be sure the Dispose() function is ever reached.

  • 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-30T13:24:41+00:00Added an answer on May 30, 2026 at 1:24 pm

    Actually, the compiler says that you are using a potentially uninitialized variable, so just initialize it:

    DisposableObject worker = null;
    try
    {
        if(/*condition*/)
            worker = new DisposableObject(/*args*/)
        else
            worker = new DisposableObject(/*other args*/)
        worker.DoStuff();
    }
    finally
    {
        if(worker != null)
            worker.Dispose();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose I have some class which has a property actor_ of type Actor .
Suppose we have some class A that implements some interface I I i =
Suppose I have a class Base which has a member variable A* my_hash. I
Suppose I have a class MyClass to which I want to add certain 'observer'
Suppose I have a class that implements an interface: public class A implements IB
Suppose I have a class RainbowColorsMapper with the constructor RainbowColorsMapper(int n) , where n>=2.
Suppose we have a class A and class B and C inherit from it.
Suppose I have a class with a constructor (or other function) that takes a
Suppose I have: class Foo { ... }; class Bar : public Foo {

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.