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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:29:02+00:00 2026-05-20T15:29:02+00:00

I like the using(){} statement for its control of scope and for readability. Not

  • 0

I like the using(){} statement for its control of scope and for readability. Not only can you create objects, use them and dispose them neatly, but you can also use it like this:

Suppose myInstance is an instance of MyClass from some other place in code – ie a method parameter or something

using (var t = myInstance) {
    t.Foo= "Hello";
    t.Bar= "World";
    ...
}

Definition of MyClass:

public class MyClass : IDisposable
{
   public string Foo {get; set;}
   public string Bar {get; set;}
   ...

   public void Dispose() {}
}

This makes code neater and more readable (I feel) and certainly easier to type. However in order to use this widely you have to implement IDisposable. In this case there’s no need for MyClass actually handle anything in Dispose, so its empty.

My question is, is there any drawback to using IDisposable in this way?

I appreciate that there are similar questions already on SO, but I don’t think these deal with using IDispose for this purpose (or they refer to specific classes).

EDIT:

OK, so I guess I was being a bit specifc, but there’s a wider point here. The using statement is a handy, very neat way to define an instance for a specific length of time, and then you can forget about it. Its seems unfair that this ability should be restricted to IDisposable objects only. Yes there’s a certain amount of laziness in not just instancing and then setting to null, but the using block makes it very specific what the instance’s scope is. Why shouldn’t I be allowed to do it with non IDisposable classes?

  • 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-20T15:29:03+00:00Added an answer on May 20, 2026 at 3:29 pm

    When you implement IDisposable you’re advertising that your class is special and needs to be disposed off properly. Not only with inline code like that but also when classes hold references to your class. They’ll also need to implement IDisposable in order to dispose of your instance. That creates an artificial requirement on the users of your class.

    You can use a scope to achieve what you want:

    {
      var _ = instance;
      _.Foo = "Hello"; 
      _.Bar = "World"; 
    }
    

    Another option is this:

    instance.With(_ => {
      _.Foo = "Hello";
      _.Bar = "World"; 
    });
    
    ...
    
    public static class WithExtensions {
      public static void With<T>(this T instance, Action<T> action) {
        action(instance);
      }
    }
    

    This one is better because what’s going on is more explicit.

    I also see the pain that you have. It’d be better if we could define our own control blocks (like in Nemerle). Although C# doesn’t allow that kind of customization, you shouldn’t abuse the specific using statement to achieve what you want. You can use lambdas to do that, much in the way I’ve shown with the With extension method. In fact, that’s how some parallel “constructs” were done in the Task Parallel Library.

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

Sidebar

Related Questions

Can you use 2 'using' statements like: using (SqlConnection ..) { using(SqlDataReader reader =
I am using CASE Statement in Stored procedure. I am using like create proc
I am using this statement in part of my application, its generated. The LIKE
I have a using statement defined like this in codebehind: using Name = Extremely.Long.And.Unwieldy.Namespace
When using a statement like #if TRUE , what should I expect to happen?
I am using a simple try-with-resources statement like the following: try (final CSVWriter w
I am using to write a select query and the value for like statement
I'm using iBATIS to create select statements. Now I would like to implement the
I've been wondering about alternative ways to write control structures like you can write
I'd just like to verify if using prepared statements in MySQL prevents SQL injection.

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.