What is the scope of the operator without curly braces

I inherited the following code:

using (var dataAccessConnection = da.GetConnection()) //no opening curly brace here using (var command = new SqlCommand(sql, dataAccessConnection.Connection)) { command.CommandType = CommandType.Text; using (var sqlDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)) { while (sqlDataReader.Read()) { rueckgabe.Add(new Myclass { Uid = Guid.NewGuid(), ImportVersionUid = versionUid, MyProperty = Convert.ToInt32(sqlDataReader["MyProperty"]) }); } } command.Connection.Close(); dataAccessConnection.Connection.Close(); } 

Looking at the code, I explicated an open brace after a suggestion for use.

The code compiles and does what is expected. The application behaves unpredictably. At some point, he cannot access the database server.

Does this code make sense? Does dataAccessConnection have full scope?

+6
source share
2 answers
Operators

using without explicit curly braces applies only to the following statement.

 using (Idisp1) // use it // it disposed 

Thus, when confined, they work the same way. The second using here acts as a single statement.

 using (Idisp1) using (Idisp2) { } 

Comment stakx suggested that formatting make it clear how the compiler reads usage blocks. In fact, they are usually formatted as detected by the OP:

 using (Idisp1) using (Idisp2) { } 

This is equivalent to this:

 using (Idisp1) { using (Idisp2) { } } 

Please note that the first at the top is always the last to be posted. Thus, in all previous examples, Idisp2.Dispose() is called before Idisp1.Dispose() . This does not apply in many cases when you do something similar, but I believe that you should always know what your code will do and make an informed decision not to care.

An example of this is reading a web page:

 HttpWebRequest req = ...; using (var resp = req.GetResponse()) using (var stream = resp.GetResponseStream()) using (var reader = new StreamReader(stream)) { TextBox1.Text = reader.ReadToEnd(); // or whatever } 

We get the answer, get the stream, get the reader, read the stream, delete the reader, delete the stream, and finally delete the answer.

Note that commentator Nikhil Agrawal noted that this is a language function related to blocks not related to the using keyword. For example, the same applies to if blocks:

 if (condition) // may or may not execute // definitely will execute 

Vs

 if (condition1) if (condition2) // will execute if both are true // definitely will execute 

Although you should never, of course, use if in a way that is terrible to read, but I thought it would help you understand the using case. I am personally very good with using chains.

+10
source

The C # language specification ( Version 5 ) describes the using statement as:

using a conditional statement:

using ( resource collection ) built-in statement

I.e:

The using statement takes one or more resources, executes the statement , and then deletes the resource.

( My emphasis )

So, how do we end up using it with curly braces? Since the definition of the built-in operator:

submerged statement:

block
empty expression
Expression expression
Expression select
iteration operator
jump-expression
fitting rooms expression
registered expression
unregistered-expression
Expression Lock
using-expression
output operator

and

The built-in operator nonterminal is used for operators that appear in other operators

And finally, we find that the block is defined as:

The block allows you to write multiple statements in contexts where one statement is allowed.

block:

{ statement-list opt }

Thus, basically, curly braces can always be used to accept a situation where one operator is accepted and instead has several operators.

It so happened that almost always we want to use more than one statement, so curly braces are usually considered as part of if , using , etc. operators. While in reality they are a separate part of the language.

0
source

Source: https://habr.com/ru/post/972437/


All Articles