Does the operator use me to close or destroy objects?

If I use something like:

using (OdbcConnection conn = new OdbcConnection(....))
{
  conn.open();
  my commands and sql, etc.
}

Should I do conn.close (); or is an expression used so that I don't make this last call? Does it install everything in the use block? For example, if I called other objects that were not related to each other, would it also automatically scatter them?

Thanks. After I read about usage on the Microsoft website, I was unclear. I want to make sure that I have no memory leaks.

+3
source share
3 answers
  • The used block will get rid of OdbcConnection.
  • Normal scope rules work for everything that is declared inside a use block.
  • IDisposable.
    • , , .

. using # .

, () , , . , . .


[] : , :

using (Bitmap b1 = new Bitmap("file1"), b2 = new Bitmap("file2")) 
{ ... }

, , , for if. . , .

using(Bitmap b = new Bitmap("filex"))
using(Graphics g = Graphics.FromImage(b))
{ 
}

error CS1044: Cannot use more than one type in a for, using, fixed, or declaration statement.

// error CS1044
using(Bitmap b = new Bitmap("filex"), Graphics g = Graphics.FromImage(b)) 
+6

using Close Dispose .

using.

+4

The using statement ensures that an object that implements IDisposable is deleted. It will only remove the object that references the usage block, so your code is basically equivalent:

OdbcConnection conn = new ....;
try
{
   conn.open();
   conn.....
}
finally
{
   conn.dispose();
}
+1
source

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


All Articles