1

The C# reference manual defines the syntax of using statements as:

using-statement: using ( resource-acquisition ) embedded-statement resource-acquisition: local-variable-declaration expression 

Can someone provide me with an example that uses an expression inside using statement. Conventional examples are using a variable declaration.

5
  • 4
    var res = GetResource(); using (res) { ... } This style is not recommended because res is still in scope after the using block, but has been disposed. Commented Oct 3, 2014 at 0:03
  • So, only an identifier? To me the grammar looks very permissive, and it looks like at the syntactic level all expressions are allowed. Commented Oct 3, 2014 at 0:06
  • @Ali: "only an identifier?" --- it's an expression, as you asked. Commented Oct 3, 2014 at 0:07
  • Sure, it is! I was wondering if there is any more complex, say conditional expressions, is allowed. Commented Oct 3, 2014 at 0:09
  • 3
    Yes, they are. Any expression is allowed. I was just providing one example. You could as well using (GetResource()) or using (flag ? resA : resB) etc. Commented Oct 3, 2014 at 0:10

4 Answers 4

3

An example of using an expression is as follows:

var stream = new MemoryStream(); using(stream) { //Perform operations with stream here. } //Stream is now disposed. 

Here, the stream variable is declared outside the using, but wrapped by the using. It will then dispose the stream variable upon completion.

This isn't a very common pattern, but it is useful for where you may need to perform other operations on the resource after it has been disposed of.

This can be used with method calls, etc. Essentially any expression that evaluates to a type of IDisposable can be used. Generally, not having access to the IDisposable inside the using statement isn't that useful.

The only scenario where I have seen it used this way is in ASP.NET MVC with the form helpers such as.

@using(Html.BeginForm()) { @Html.TextBox("Name"); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Reading the documentation you linked I see, "You can instantiate the resource object and then pass the variable to the using statement, but this isn't a best practice. In this case, after control leaves the using block, the object remains in scope but probably has no access to its unmanaged resources." Which supports the usage pattern I outlined above. It's not best practice, but the answer is also not "totally wrong."
1

An example:

//Create the file. using (FileStream fs = File.Create(path)) { AddText(fs, "This is some text"); AddText(fs, "This is some more text,"); AddText(fs, "\r\nand this is on a new line"); AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i=1;i < 120;i++) { AddText(fs, Convert.ToChar(i).ToString()); } } 

Explanation:

Provides a convenient syntax that ensures the correct use of IDisposable objects.

The using statement calls the Dispose method on the object at the and of the block.

It will call the Dispose method even if an Exception is thrown.

1 Comment

this is an example of local-variable-declaration not expression
0

The key thing to look at is actually the definition of local-variable-declaration from section 8.5.1

A local-variable-declaration declares one or more local variables.

local-variable-declaration:
local-variable-type   local-variable-declarators

local-variable-type:
type
var

local-variable-declarators:
local-variable-declarator
local-variable-declarators   ,   local-variable-declarator

local-variable-declarator:
identifier
identifier   =   local-variable-initializer

local-variable-initializer:
expression
array-initializer

So a local-variable-declaration could be expressed as type identifer = expression. So what the specification for using is telling you is you can either declare a variable in the form var variableName = xxxxxxx or you can just use the xxxxxx part by itself.

Comments

0

Another example of expression that I don't see mentioned here is:

IDisposable disposable; using (disposable = new SomeDisposableObject()) { // do something } 

or

IDisposable disposable; using (disposable = SomeExpressionOrFunctionThatEvaluatesToADisposable()) { // do something } 

The reference doesn't have to be of type IDisposable, it could be any type that implements it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.