Posted in

The ‘using’ Statement in C# Just Got Smarter — Are You Using It?

dot net using keyword

The using keyword is very familiar to C# developers. We think, “This is for Disposable Resources, nothing more.” But these days, using in C# has gotten a lot smarter — and many people don’t fully understand it!

Did you know that C# 8.0 introduced a new form of using that makes code cleaner, shorter, and bug-free?

Come on, today we’ll learn about using’s old mistakes, new features, and how you can take it to the next level.

What is the main purpose of using?

There are many classes in C# that use unmanaged resources (e.g. File Handle, Database Connection, Network Stream). The lifetime of these resources needs to be managed properly.

That’s why C# has the IDisposable interface and you can auto-dispose those classes with the using statement.

using (var stream = new FileStream("data.txt", FileMode.Open))
{
// Use stream
}
// Here stream.Dispose() will be called automatically.

Mistakes that developers make

❌ Mistake 1: Not doing Dispose() manually

var conn = new SqlConnection("...");
conn.Open();
// If you don't Dispose(), the connection remains open!

❌ Mistake 2: Writing Python-style code for nested using

using (var stream = new FileStream("data.txt", FileMode.Open))
{
using (var reader = new StreamReader(stream))
{
string line = reader.ReadLine();
}
}

👉 No need to write it now! The new using statement in C# 8.0 has solved this.


Using C# has become smarter since 8.0

C# 8.0 introduces the “using declaration” — instead of having to write braces {} like before, you can just write it on one line:

using var stream = new FileStream("data.txt", FileMode.Open);
var reader = new StreamReader(stream);
string line = reader.ReadLine();

Benefits:

  1. Code is smaller
  2. Nesting is reduced
  3. Code is cleaner and readable
  4. Disposes automatically when scope ends

So now there are two styles of using

Real-life example

✅ Previous method

using (var client = new HttpClient())
{
var response = await client.GetAsync("https://example.com");
Console.WriteLine(response.StatusCode);
}

✅ New method

using var client = new HttpClient();
var response = await client.GetAsync("https://example.com");
Console.WriteLine(response.StatusCode);

👉At the end of the scope, client.Dispose() will be called automatically.


When should you use which one?


Advance Tip: Create Custom Disposable Class

class MyResource : IDisposable
{
public void Dispose()
{
Console.WriteLine("Resource cleaned up");
}
}
using var resource = new MyResource();
Console.WriteLine("Using resource");

Output:

Using resource
Resource cleaned up

👉 Now you can add Dispose behavior to your own class if you want.

Extra: async version of IDisposable

From C# 8.0 onwards:

await using var myAsyncResource = new MyAsyncResource();

Using await can give you asynchronous resource management!


Conclusion — What did you learn?

🔹 using is now smarter than ever
🔹 From C# 8.0, using var allows you to write cleaner code
🔹 You can automate Dispose instead of doing it manually
🔹 Readability increases, bugs decrease
🔹 Using Async Disposable speeds up modern async code

We often overlook small keywords — but that’s where the real secret of Clean Code lies.

Using is no longer just syntax sugar — it’s the ultimate tool for resource management. How do you use using? What features of C# 8.0 are you using? Let us know in the comments below!

Leave a Reply

Your email address will not be published. Required fields are marked *