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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:40:19+00:00 2026-05-21T21:40:19+00:00

Is it possible to use using syntax to trigger all objects that implement IDisposable

  • 0

Is it possible to use using syntax to trigger all objects that implement
IDisposable interface call the corresponding Dispose() method?

For example, if the objects of ObjectOne and ObjectTwo exist in the section of interest, can we make it the way so that the Dispose() method of both objects will be called automatically. As the example shown, I knew how to do it for one Class type, but I don’t know how to do this kind of trick for more than one Class type. Because the following syntax is not allowed in C#.

// This is not a valid C# statement

using( ObjectOne one = new ObjectOne();
       OjbectTwo two = new ObjectTwo() )
{
   ...  

  // hopefully, the Dispose methods of both **one** and **two** will be called.
}

A concrete example to illustrate how to trigger the auto-calling Dispose method for just one class type.

namespace ConsoleApplication1
{
    class ObjectOne : IDisposable
    {
        public string ObjectName { get; set; }

        public ObjectOne() : this("Empty") { }
        public ObjectOne(string objName) 
        {
            ObjectName = objName;
        }

        public void Dispose()
        {
            Console.WriteLine("ObjectOne.Dispose " + ObjectName);
        }
    }

    class ObjectTwo : IDisposable
    {
        public string ObjectTwoName { get; set; }

        public ObjectTwo() { }
        public ObjectTwo(string objName)
        {
            ObjectTwoName = objName;
        }

        public void Dispose()
        {
            Console.WriteLine("ObjectTwo.Dispose " + ObjectTwoName);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("before");

            // make sure that the Dispose method of object one1 and one2 is called
            using (ObjectOne one1 = new ObjectOne(),
                             one2 = new ObjectOne()
                   )
            {
                // section of interest
                Console.WriteLine("middle");
            }

            Console.WriteLine("after");
            Console.ReadLine();
        }
    }
}

Thank you.

  • 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-21T21:40:20+00:00Added an answer on May 21, 2026 at 9:40 pm

    You can nest using statements:

    using( ObjectOne one = new ObjectOne())
       using( OjbectTwo two = new ObjectTwo() )
       {
          ...  
    
         // hopefully, the Dispose methods of both **one** and **two** will be called.
       }
    

    EDIT: the only other way would be to implement a custom list of IDisposables:

    public class DisposableList : List<IDisposable>, IDisposable
    {
       //this is all you need if you will ONLY use it in a using statement or explicitly call Dispose;
       //there is a more developed pattern that performs disposal automatically on finalization.
       public void Dispose()
       {
          foreach(var disposable in this)
             disposable.Dispose();
       }
    }
    
    ...
    
    using(var disposables = new DisposableList{new ObjectOne(), new ObjectTwo()})
    {
       //unfortunately, you must now cast the items back to their types 
       //in order to use them as anything but IDisposables.
       var objectOne = (ObjectOne)disposables[0];
       var objectTwo = (ObjectTwo)disposables[1];
    
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use the syntax using(_mocks.Record()) { //... } using(_mocks.Playback()) { //...
Is it possible to create Selenium tests using the Firefox plugin that use randomly
When adding a method to a PSObject using Add-Member is it possible to use
Is it possible to use the using declaration with template base classes? I have
I wonder if is possible to use FTS with LINQ using .NET Framework 3.5.
Is it possible to use the Flex Framework and Components, without using MXML? I
Is it possible to use the Bindable compiler setting when using mxml? I've got
I have recently started looking into Google Charts API for possible use within the
Possible Duplicate: Use SVN Revision to label build in CCNET I'm working through the
Is it possible to use a flash document embedded in HTML as a link?

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.