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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:47:50+00:00 2026-05-22T02:47:50+00:00

why variable (myform) in using block treated as read-only and the compiler raise an

  • 0

why variable (myform) in using block treated as read-only and the compiler raise an error when I try to pass it as a reference to a function.

sample code:

using (Form myform = new Form)
{
    myfunc(ref myform);
}

passing using variable to a function as ref will raise an error. thus the code above will raise an error.

Note : ‘readonly’ keyword is unrelated to my question.

  • 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-22T02:47:51+00:00Added an answer on May 22, 2026 at 2:47 am

    I’m looking at an (outdated?) spec [1] right now.

    15.13 says that variables that you declare in the resource acquisition part are readonly. That is:

    var form = new Form1();
    using (form) {
        form = null;
    }
    

    works, but

    using (var form = new Form1()) {
        form = null;
    }
    

    doesn’t.
    This answers part of the question (i.e. Why? Because it is part of the spec..), but I understand that this is not really satisfying. But why would you even want to do that?


    Edit: After thinking about this, let me offer a possible explanation for this rule:

    You have

    using (var something = new Foo()) {
       something = /* whatever */
    }
    

    and the compiler allows this. Now what if Foo needs a lot of unmanaged resources (maybe that’s the reason you wanted to use using in the first place)? After the using block you have no way to access this reference anymore. It wasn’t disposed, because you reassigned something and forgot to handle it yourself. You don’t have a guarantee that the GC runs, at all. Or when. You just created a resource leak that is obscured and hidden.


    A final one, inspired by Henk’s link to Eric Lippert’s blog, which again just ends up throwing the spec at us:

    A using statement of the form

    using (expression) statement

    has the same two possible expansions, but in this case ResourceType is
    implicitly the compile-time type of the expression, and the resource variable
    is inaccessible in, and invisible to, the embedded statement.

    In other words:

    var form = new Form1();
    using (form) {
        form = null;
    }
    

    works, because this is expanded to

    var form = new Form1();
    var invisibleThing = form;
    try {
       form = null;
    } finally {
        if (invisibleThing != null) ((IDisposable)invisibleThing).Dispose();
    }
    

    So in this case the fact that you have no influence over the using reference is just hidden from you and is exactly like in the previous case.

    1:http://www.ecma-international.org/publications/standards/Ecma-334.htm

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

Sidebar

Related Questions

I´d like to pass a javascript variable to php and I thought of using
How can I unset variable? For example, PHP has an unset($var) function.
I am using jQuery and trying to load a variable in place of a
I want to pass JavaScript variables to PHP using a hidden input in a
I am having a hard time using the $op variable when working with forms.
I need to pass an instance variable (self.rank) to be used by a class
I am using this to detect errors on my form... var error = false;
Static variable gotcha in php I am from Java background and have switched to
I have a perl variable $results that gets returned from a service. The value
If I have a variable in C# that needs to be checked to determine

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.