C# using keyword, is used to manage resources and ensure that they are properly disposed of when they are no longer needed. The using
keyword is used to declare a scope within which one or more objects are used. These objects can include file streams, database connections, and other resources that require careful management. When the scope is exited, the objects declared in the using
statement are automatically disposed of.
For example, suppose you are working with a file stream to read data from a file. You can use the using
statement to ensure that the file stream is properly disposed of when you are done with it:
1 2 3 4 |
using (FileStream fileStream = new FileStream("example.txt", FileMode.Open)) { fileStream.Read(...); } |
In this example, the using
statement declares a new instance of the FileStream
class, which is used to open the example.txt
file for reading. The code inside the using
statement can then be used to read data from the file. Once the code inside the using
statement has completed, the file stream is automatically disposed of, even if an exception occurs. This ensures that the file is closed and any system resources associated with it are released. The following code is the traditional equivalent of the one on top:
1 2 3 4 5 6 7 8 9 |
FileStream fileStream = new FileStream("example.txt", FileMode.Open);; try { fileStream.Read(...); } finally { fileStream.Dispose(); } |
The using
keyword can also be used with other types of resources, such as database connections:
1 2 3 4 5 6 7 |
using (SqlConnection connection = new SqlConnection(connectionString)) { // Open the database connection connection.Open(); // Execute database commands } |
In this example, the using
statement declares a new instance of the SqlConnection
class, which is used to connect to a database using a connection string. The code inside the body can then be used to execute commands on the database. If you want to skyrocket your C# career, check out our powerful ASP.NET full-stack web development course that also covers test-driven development.
When the code inside the body has completed, the database connection is automatically closed and any resources associated with it are released.
In addition to managing resources, the using
keyword can also improve the readability of your code. By declaring the scope in which a resource is used, you make it clear to other developers which parts of your code are responsible for managing that resource.
using
keyword is a syntactic sugar for the correct use of IDisposable
s to manage resources and ensure that they are properly disposed of when they are no longer needed. Using the using
keyword, you can write readable code, and avoid potential resource leaks and other issues.
Using statements in new C#
The following is another syntax for using statements.
1 2 3 4 |
string ReadFile(){ using var fileStream = new FileStream("example.txt", FileMode.Open); // Use the file stream variable } |
In this example, the fileStream
is disposed at the and of the ReadFile
method’s block even if exceptions occur in the block. You can also learn more about exception handling in this article.
Multiple Using Statements
You can also declare and manage multiple resources like the followong example:
1 2 3 |
using var fileStream = new FileStream("example.txt", FileMode.Open); using var databaseConnection = new SqlConnection(connectionString); // Use the file stream and database connection variables |
In this example, the using
statement declares a file stream and a database connection, and assigns them to the fileStream
and databaseConnection
variables, respectively. Both resources can then be used in the code block that follows the using
statement. Imagine that how dirty the code would end up if we implement the same thing using multiple try-finally
blocks. By the way, did you know that we offer a unique online course that boosts your C# career? Check it out here!
Summary
In summary, the using
statement in C# has been expanded and enhanced in recent releases. These new features make it easier to manage resources, import namespaces globally, and declare and manage multiple resources in a single block. These enhancements make C# a more powerful and efficient language for managing resources in your applications.