Using the using statement

Possible duplicates:
Which is better, and when: use an operator or call Dispose () on IDisposable in C #?


When should I use block usage in C #?


using the if if statement?

Right, how can I use the using statement? I have a textbook open and I don’t understand it. And I can see more than 1 different implementation methods. What is the correct or preferred way?

+1
source share
5 answers

The using statement is used for any object that implements IDisposable.

 using (var my_object = new IDisposableObject ()) { //do my_object code here. } //once the program reaches here, it calls my_object.Dispose(); 

Typically, this is used for objects with connections that need to be manually processed (closed) when the program terminates with them. For example, open file and database connections.

The using statement will call Dispose, even if there is an error in the code, so it is akin to calling the Dispose method in the finally block of the try catch statement.

Example / Tutorial

+9
source

This is a shorter syntax to make sure that dispose is called:

 using (File f = File.Open("...")) { } 

coincides with

 File f; try { f = File.Open("..."); } finally { f.Dispose(); } 
+1
source

There are two main ways to use the using statement. As extracted from using directive (C #) from MSDN.

  • Create an alias for the namespace (using an alias).
  • Allow the use of types in the namespace, so you do not need to qualify the use of types in this namespace (using directive).
0
source

To expand Kevin's answer, the using statement effectively completes the creation of your object in a try / finally block that calls the Dispose () object in the finally ie section

 using(myObject m = new myObjecyt()) { // Code here } 

coincides with

 myObject m = new myObjecyt() try { // Code here } finally { m.Dispose(); } 

this can be verified by checking MSIL.

0
source

The Usage key helps you do something that needs to be done safely and clearly in .net. This property is the placement of certain objects. You may have learned how we have garbage collection in .Net, which means that for many objects we do not need to take care of them when we have finished using them. However, for another object, a method called Dispose must be called. Best practice is that whenever an object has a Dispose method, we must call this method when we are done with this object.

(They typically handle unmanaged resources . This means that it uses memory or other parts of the computer that are outside of the control of the .NET runtime. Therefore, when garbage collection reaches a dropped .Net object, it cannot efficiently release these resources. which may cause a memory leak and all other problems. A good example is ADO.NET Connection repeatedly. Removing your connection objects may cause database problems.)

Using key is also tied to this "Dispose" method. More precisely, when an object has a Dispose method, we either call ( A. ). Ask when we are done with it, or ( B ). put our code that uses this object in the Usage block. The Usage block does a few things for you:

  • When the code exits the block, the important Dispose method is automatically called for you.
  • More importantly, if there is an error in the code block, the Dispose method will still be called. This is why the use block is really useful. Otherwise, you will have to add a lot of error handling code.

The key is that for many of these objects that have a Dispose method, error handling is especially high . For our code, we do not need error handling; The consequences of the error occurring are not really a problem. But for these IDisposable objects, errors are often a problem, maybe a big problem. As such, .Net provides syntax for busy developers to add the most basic error handling and move on. Always start with at least the Use block; you may later move on to more convenient error handling, but at least you have this basic security.

Here is a good explanation of the Using keyword .

0
source

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


All Articles