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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:43:28+00:00 2026-06-08T17:43:28+00:00

Suppose I have the following disposable class and example code to run it: public

  • 0

Suppose I have the following disposable class and example code to run it:

public class DisposableInt : IDisposable
{
    private int? _Value;

    public int? MyInt
    {
        get { return _Value; }
        set { _Value = value; }
    }

    public DisposableInt(int InitialValue)
    {
        _Value = InitialValue;
    }

    public void Dispose()
    {
        _Value = null;
    }
}

public class TestAnInt
{
    private void AddOne(ref DisposableInt IntVal)
    {
        IntVal.MyInt++;
    }

    public void TestIt()
    {
        DisposableInt TheInt;
        using (TheInt = new DisposableInt(1))
        {
            Console.WriteLine(String.Format("Int Value: {0}", TheInt.MyInt));
            AddOne(ref TheInt);
            Console.WriteLine(String.Format("Int Value + 1: {0}", TheInt.MyInt));
        }
    }
}

Calling new TestAnInt().TestIt() runs just fine. However, if I change TestIt() so that DisposableInt is declared inside of the using statement like so:

    public void TestIt()
    {
        // DisposableInt TheInt;
        using (DisposableInt TheInt = new DisposableInt(1))
        {
            Console.WriteLine(String.Format("Int Value: {0}", TheInt.MyInt));
            AddOne(ref TheInt);
            Console.WriteLine(String.Format("Int Value + 1: {0}", TheInt.MyInt));
        }
    }

I get the following compiler error:

Cannot pass 'TheInt' as a ref or out argument because it is a 'using variable'

Why is this? What is different, other than the class goes out of scope and to the garbage collector once the using statement is finished?

(and yeah, I know my disposable class example is quite silly, I’m just trying to keep matters simple)

  • 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-06-08T17:43:30+00:00Added an answer on June 8, 2026 at 5:43 pm

    Why is this?

    Variables declared in using statements are read-only; out and ref parameters aren’t. So you can do this:

    DisposableInt theInt = new DisposableInt(1);
    using (theInt)
    {
        AddOne(ref theInt);
    }
    

    … but fundamentally you’re not using the fact that it’s a ref parameter anyway…

    It’s possible that you’ve misunderstood what ref really means. It would be a good idea to read my article on parameter passing to make sure you really understand.

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

Sidebar

Related Questions

Suppose I have following code package memoryleak; public class MemoryLeak { public static int
suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int
Suppose I have the following hierarchy: class A { public: A() private: int aa;
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();
Suppose I have following code: public class CBase: AbstractC,IRenderable { //code here } public
Suppose we have following two classes: class Temp{ public: char a; char b; };
Suppose we have following code defined in tester.py class Tester( object ): def method(
Suppose I have following code: def foo(s): A dummy function foo. For example: >>>
Suppose I have following code def foo(x:Int):Unit = { if (x == 1) println
let's suppose to have following code: <body style=overflow: hidden> <div class=wall> <button>New</button> <div class=prova>

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.