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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:51:29+00:00 2026-05-10T14:51:29+00:00

In C#, if I want to deterministically clean up non-managed resources, I can use

  • 0

In C#, if I want to deterministically clean up non-managed resources, I can use the ‘using’ keyword. But for multiple dependent objects, this ends up nesting further and further:

using (FileStream fs = new FileStream('c:\file.txt', FileMode.Open)) {     using (BufferedStream bs = new BufferedStream(fs))     {         using (StreamReader sr = new StreamReader(bs))         {             // use sr, and have everything cleaned up when done.         }     } } 

In C++, I’m used to being able to use destructors to do it like this:

{         FileStream fs('c:\file.txt', FileMode.Open);     BufferedStream bs(fs);     StreamReader sr(bs);     // use sr, and have everything cleaned up when done. } 

Is there a better way in C# to do this? Or am I stuck with the multiple levels of nesting?

  • 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. 2026-05-10T14:51:30+00:00Added an answer on May 10, 2026 at 2:51 pm

    You don’t have to nest with multiple usings:

    using (FileStream fs = new FileStream("c:\file.txt", FileMode.Open)) using (BufferedStream bs = new BufferedStream(fs)) using (StreamReader sr = new StreamReader(bs)) {     // all three get disposed when you're done } 

    In .NET Core, there’s a new using statement which allows you to dispense with the parentheses, and the disposal happens at the end of the current scope:

    void MyMethod() {     using var fs = new FileStream("c:\file.txt", FileMode.Open);     using var bs = new BufferedStream(fs);     using var sr = new StreamReader(bs);     // all three are disposed at the end of the method } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Don't declare variables above the block where they'll be used… May 11, 2026 at 11:23 am
  • added an answer Set the HTTP request timeout. May 11, 2026 at 11:23 am
  • added an answer Try sending with 8-bit encoding: $message->setTextEncoding(new EightBitEncoding()); $message->setHTMLEncoding(new EightBitEncoding()); May 11, 2026 at 11:23 am

Related Questions

In C#, if I want to deterministically clean up non-managed resources, I can use
In C# if I want to convert a double (1.71472) to an int then
In C# if I want to create a Custom Event you do something like
In C#, if I have an inherited class with a default constructor, do I
In C#, if I need to open an HTTP connection, download XML and get
In C#, if I declare an auto-implemented property, why do I have to declare
If I have a variable in C# that needs to be checked to determine
If i were developing a screensaver using a windows.form in C# how would i
If I define a struct in C# using automatic properties like this: public struct
If I'm using an ArrayList in C#.NET, is the order guaranteed to stay the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.