Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Why can't I pass a class declared in a using statement as a reference type?

Tags:

c#

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)

like image 978
Irinotecan Avatar asked Jul 24 '12 17:07

Irinotecan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

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.

like image 151
Jon Skeet Avatar answered Nov 13 '22 20:11

Jon Skeet