Search results
Results from the WOW.Com Content Network
The using statement will call conn.Close() automatically due to the using statement (using (SqlConnection conn = new SqlConnection(connString)) and the same for a SqlDataReader object. And also if any exception occurs it will close the connection automatically. For more information, see Usage and Importance of Using in C#.
The using statement is used to work with an object in C# that implements the IDisposable interface. The IDisposable interface has one public method called Dispose that is used to dispose of the object. When we use the using statement, we don't need to explicitly dispose of the object in the code, the using statement takes care of it.
The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned. The using statement ensures that Dispose is called even if ...
From MSDN, using Statement (C# Reference) The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the ...
From the C# x in a Nutshell books which explains the using statement very well: The .NET Framework defines a special interface for types requiring a tear-down method: public interface IDisposable { void Dispose(); }
According to Microsoft's C# reference, "You can instantiate the resource object and then pass the variable to the using statement, but this is not a best practice. In this case, the object remains in scope after control leaves the using block even though it will probably no longer have access to its unmanaged resources."
I see plenty of other answers indicated when you should have a using statement. I want to address when specifically should not have a using statement: If you need to use your object outside of the scope of the current function, don't have a using block. Good example are a factory method that returns a database connection or a method that needs ...
C# 10.0 introduces a new feature called global using directive (global using <fully-qualified-namespace>;) which allows to specify namespaces to be implicitly imported in all files in the compilation. .NET 6 RC1 has this feature enabled by default in new project templates (see the <ImplicitUsings>enable</ImplicitUsings> property in your .csproj).
I've read and I believe I understand what C#'s using statement does (please correct me if I'm wrong): Initializes an IDisposable object as read only to a limited scope (the using block). I know you can initialize before the using and that doesn't limit the scope, but that is advised against here:
From using Statement (C# Reference) by MSDN. Defines a scope, outside of which an object or objects will be disposed. The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface.