The using statement is a powerful utility for automatic disposal of unused resources.
The base implementation works like this:
using (var ch = new CustomHandler())
{
/* Do something with ch */
}The using statement is useful for preventing memory leaks by handling unmanaged resources like:
- Closing files that are no longer being used
- Closing database connections
- Handling HTTP client termination, etc.
In order to use the using statement for a given class, the class must implement the IDisposable interface:
class CustomHandler : IDisposable
{
public void SayHello()
{
Console.WriteLine("Hello World!");
}
// coming from IDisposable
public void Dispose()
{
Console.WriteLine("Handled dispose!");
}
}The automatic disposal can be verified by throwing an exception in the SayHello() method.
class CustomHandler : IDisposable
{
public void SayHello()
{
throw new Exception("Something went wrong!");
}
public void Dispose()
{
Console.WriteLine("Handled dispose!");
}
}I created a new class Main that creates an instance of CustomHandler().
Without Using
First test, no using.
public class Main
{
public void LearnUsing()
{
var ch = new CustomHandler();
ch.SayHello();
}
}
var m = new Main();
m.LearnUsing();The exception is thrown and the process is terminated as expected, but no resources were disposed.
Unhandled exception. System.Exception: Something went wrong!
Process finished.With Using
public class Main
{
public void LearnUsing()
{
using (var ch = new CustomHandler())
{
ch.SayHello();
}
}
}
var m = new Main();
m.LearnUsing();This time around the resources are disposed successfully.
Unhandled exception. System.Exception: Something went wrong!
Handled dispose! <<<<<<<
Process finished.Manual Disposal
By this point you probably asked yourself a question: why not use a try-catch-finally block? That works too, but try-catch does not dispose resources by itself. It only handles errors.
In order to dispose resources in try-catch, you would need to manually call the Dispose() method:
var ch = new CustomHandler();
try
{
ch.SayHello();
}
catch (Exception ex)
{
/* Handle exception */
}
finally
{
// Manual disposal
ch.Dispose();
// I'm using finally here because it will be invoked
// regardless if SayHello() is a success or an exception
}The using statement, on the other hand, calls the Dispose() method automatically.
Using in Existing Classes
As mentioned, any class that implements the IDisposable interface can be used within the using block.
FileStream
using (FileStream fs = new FileStream("greetings.txt", FileMode.Open))
{
/* Write to file */
}HTTPClient
public async Task LearnUsing()
{
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync("/api/getData");
if (response.IsSuccessStatusCode)
{
/* Handle response */
}
}
}Just make sure you prefix the call to LearnUsing() with the await keyword since it is now a Task.
await m.LearnUsing();SQL Connection
The using statement is also used when executing database queries without using any frameworks. To get started, install the System.Data.SqlClient package via NuGet package manager and import it as a dependency.

Then set up a connection string to your DB. For this, I created a local DB via SQL Server.

To connect to your local DB, set up a connection string in your C# application.
string connectionString = "Server=<Server-Name>;Database=<Database-Name>;Integrated Security=True;";
// or in my case
string connectionString = "Server=DESKTOP-F5;Database=Games;Integrated Security=True;";Now using a combination of using statements:
- Establish a connection with the database (SqlConnection)
- Execute SQL query (SqlCommand)
- Read the response from the table (SqlDataReader)
Here you can see how to nest the using statements.
string connectionString = "Server=DESKTOP-F5;Database=Games;Integrated Security=True;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
await connection.OpenAsync();
string sqlQuery = "SELECT * FROM Game_Types";
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
using (SqlDataReader reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
Console.WriteLine(reader["Title"]); // Multiplayer
// prints row data for each iteration
}
}
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}Note: If you're using Entity Framework, queries do not need to be wrapped with using. The DbContext class automatically disposes resources.
For immediate disposal, the using statement can also be used inline (without curly braces).
using (var fs = new FileStream("my-file.txt", FileMode.Open))
Console.WriteLine("File open!");That's all I have for today. Don't forget to hit the follow button. Also, follow me on Twitter to stay up to date with my upcoming content.
Bye for now.
