Basic C # on the "use" key

I'm starting to learn C #.

I understand that using is similar to import in C #

But then I came across in this situation:

 using (con) { con.Open(); cmd.ExecuteNonQuery(); } 

Just curious what this does here and what else without using (con)

+4
source share
4 answers

This form using ( statement , not ) works with the IDisposable interface to restore unmanaged resources.

Unmanaged resources are things like database connections that cannot be simply allowed for disposal by the garbage collector. Rather, they need to be closed in an orderly manner. When using code goes out of scope, the Dispose() method is called on the database connection object, closing the connection and freeing the resource.

Check out the SQLConnection class as an example. Note that it inherits the DBConnection class, which in turn implements the IDisposable interface. The SQLConnection object implements the Dispose method , which closes the connection when the scope leaves the using block.

Note that you can use the using statement and IDisposable for fun and profit. ASP.NET MVC uses using to close HTML tags!

+11
source
Operator

using will call the Dispose method at the end of the block. An object must implement an IDisposable interface in order to make it work. It works because your con object has an implementation for IDisposable , and after this method it is null . I like to implement using blocks declaring an object for the sample:

 using (var con = new SqlConnection("connection string")) { con.Open(); using(var cmd = con.CreateCommand()) { cmd.CommandText = "select count(*) from table"; result = (int) cmd.ExecuteScalar(); } con.Close(); } 
+5
source

According to MSDN

C # through the Common Language (CLR) runtime, the .NET Framework automatically frees memory used to store objects that are no longer needed. Memory release is not deterministic; memory is freed whenever the CLR decides to perform garbage collection. However, it is generally best to release limited resources as quickly as possible, such as files and network connections.

The using statement allows the programmer to specify when objects using resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should free the resources of the object.

The using statement can be completed either when the end of the using statement is reached, or when an exception is raised, and the control leaves the statement block until the end of the statement.

The using statement forces the cleaning of resources without the need to explicitly delete them to the developer or wait for the garbage collector to return the resources.

+3
source

The using block can be applied to any .NET object that implements the IDisposable interface. If you try to apply a block to an object that does not, you will get a compilation error.

The using block actually generates a try-finally block around the code in the using block.

Following:

 using (con) { con.Open(); cmd.ExecuteNonQuery(); } 

equivalent to writing:

 try { con.Open(); cmd.ExecuteNonQuery(); } finally { con.Close(); con.Dispose(); } 

Using using is a performance enhancement that provides the correct cleanup of a one-time object and, in my opinion, creates code that is easier to read and maintain.

Note. You can nest using blocks, for example:

 using(con) using(cmd) { } 

The using statement in C # is the equivalent of the Imports statement in VB.NET.

+3
source

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


All Articles