Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why c# don't let to pass a using variable to a function as ref or out [duplicate]

Possible Duplicate:
Passing an IDisposable object by reference causes an error?

Why doesn't C# allow passing a variable from a using block to a function as ref or out?

This is my code:

using (Form s = new Form())
{
    doSomthing(ref s);
}

The function ends before the using block ends, why doesn't C# let me pass s as ref or out parameter?

like image 597
Saleh Avatar asked May 14 '11 09:05

Saleh


People also ask

Why C is the best language?

The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.

Why is C used?

C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc. C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation.

Why should you learn C?

C is very fast in terms of execution time. Programs written and compiled in C execute much faster than compared to any other programming language. C programming language is very fast in terms of execution as it does not have any additional processing overheads such as garbage collection or preventing memory leaks etc.

Why is C language named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


3 Answers

using variables are treated as readonly, as any reassignment is probably an error. Since ref allows reassignment, this would also be an issue. At the IL level, out is pretty-much identical to ref.

However, I doubt you need ref here; you are already passing a reference to the form, since it is a class. For reference-types, the main purpose of a ref would be to allow you to reassign the variable, and have the caller see the reassignment, i.e.

void doSomething(ref Form form)
{
    form = null; // the caller will see this change
}

it is not required if you are just talking to the form object:

void doSomething(Form form)
{
    form.Text = "abc"; // the caller will see this change even without ref
}

since it is the same form object.

like image 171
Marc Gravell Avatar answered Oct 21 '22 13:10

Marc Gravell


The var in a using() statement is considered read-only inside the block. See § 8.13:

Local variables declared in a resource-acquisition are read-only, and must include an initializer. A compile-time error occurs if the embedded statement attempts to modify these local variables (by assignment or the ++ and -- operators) or pass them as ref or out parameters.

But note that this only applies to variables declared as part of the using statement, the following is legal (just not a good idea):

var f2 = System.IO.File.OpenText("");
using (f2)
{
    f2 = null;
}
like image 21
Henk Holterman Avatar answered Oct 21 '22 12:10

Henk Holterman


One reason could be that doSomthing could make s refer to another Form instance than the one we have created. That could introduce a resource leak since the using block would then invoke Dispose on the Form instance that came from the method, and not the one created in the using block.

like image 36
Fredrik Mörk Avatar answered Oct 21 '22 13:10

Fredrik Mörk