Introduction
This post explains how to utilize the using keyword.
using keyword
There are three major ways to use the using keyword,
- Using Directive
- Using Static Directive
- Using Statement
Here we will discuss the Using Statement.
Using Statement
- What is it?
- A statement which contains the 'using' keyword, then it is called using a statement.
- A statement which contains the 'using' keyword, then it is called using a statement.
- What it does:
- Specifies the scope or boundary of the use of a resource object (normally use curly braces, {}).
- After the end of a scope, it's called Dispose method.
- Note: If an exception occurs between this scope, then it's also called Dispose method (for more details please have a look in the 'How to work' section).
- The dispose method will free the resource object because it's no longer needed.
- In summary, every object has a life cycle (life cycle means creation and destroying the object). After destroying the object we have to release the destroyed object resources. It is done by CLR and you can control it by using a statement.
- Why do we need it?
- .NET framework is designed with an automatic memory or resource management feature which disposes, releases, or frees the object automatically.
- This process is completed through non-deterministic mode whenever the Common Language Runtime (CLR) is decided upon to perform garbage collector (GC).
- Decision
If you want to control this memory management, then we need to use a using statement. This time GC will be done by us, rather than CLR.
- Decision
- This process is completed through non-deterministic mode whenever the Common Language Runtime (CLR) is decided upon to perform garbage collector (GC).
- There are mainly two types of memory resources,
- Managed Resource
- Created by managed objects.
- Managed by CLR.
- Example: int, string bool etc variables (those are all in .NET scope).
- Decision
These manage resources are released by CLR, but if we want to release by ourselves then we need a using statement.
- Unmanaged Resource
- Created by unmanaged objects.
- Not managed by CLR.
- Example: third party libraries such as file streams, network connection, device context, database connection objects.
- Decision
These unmanaged resources are resource-intensive, so we have to release as quickly as possible. We can do this with a using statement.
- Note
Using statement is used for unmanaged resource because managed resource are released by CLR automatically.
- Managed Resource
- Simply put, .NET framework provides a using statement for memory clean-up activities.
- .NET framework is designed with an automatic memory or resource management feature which disposes, releases, or frees the object automatically.
- How to use?
- The using statement takes only one parameter, a resource that is represented by a class or stack.
- //
- // using statement syntax
- //
- using(resource, it can be a class object)
- {
- // body
- }
- // more specific syntax
- using (<ClassName> <VariableName> = new <ClassName>())
- {
- // body
- }
- //
- // using statement example
- //
- // Create a Car Class, whose has a property Name
- public class Car
- {
- public string Name { get; set; } = "Super Car";
- }
- // Call Car class from Main method
- class Program
- {
- static void Main(string[] args)
- {
- // here carObj is a resource object
- using (Car carObj = new Car())
- {
- Console.WriteLine(carObjWithUsing.Name);
- }
- }
- }
- // If you run this program then you will get a error
- // Error Number: CS1674.
- // Error Description: 'Car': type used in a using statement must be implicitly convertible to 'System.IDisposable' or implement a suitable 'Dispose' method.
- The using statement takes only one parameter, a resource that is represented by a class or stack.
- Why is this error showing?
- For using statement, the resource object must be implemeted with IDisposable interface, which is from System namespace.
- If you go to IDisposable interface you can see that here is only one method, Dispose()
- namespace System
- {
- //
- // Summary:
- // Provides a mechanism for releasing unmanaged resources.
- public interface IDisposable
- {
- //
- // Summary:
- // Performs application-defined tasks associated with freeing, releasing, or resetting
- // unmanaged resources.
- void Dispose();
- }
- }
- Here is full code with IDisposable
- // adding new code with highlighted
- public class Car : IDisposable
- {
- public string Name { get; set; } = "Super Car";
- public void Dispose()
- {
- Console.WriteLine("Disposing");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //with using statement
- using (Car carObjWithUsing = new Car())
- {
- Console.WriteLine(carObjWithUsing.Name);
- }
- }
- }
- //Output
- Super Car
- Disposing
- This Dispose method is working well now. But I have written only one: 'Console.WriteLine("Disposing");' We must write informative code to dispose.
- // adding new code with highlighted
- public class Car : IDisposable
- {
- public string Name { get; set; } = "Super Car";
- public void Dispose()
- {
- Console.WriteLine("Disposing");
- this.Name = null;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Car carObjWithUsing = new Car();
- using (carObjWithUsing)
- {
- Console.WriteLine(carObjWithUsing.Name);
- }
- // Name will be null here.
- Console.WriteLine(carObjWithUsing.Name);
- }
- }
- //Output
- Super Car
- Disposing
- Note
If you assign null to an object, it is only removing reference of the object from the stack. Object is not removed from the heap.
- The following code allows multiple instances of a type in a single statement:
- using (Car carObjWithUsing1 = new Car(), carObjWithUsing2 = new Car())
- {
- }
- Note
If you use more than one type within a single statement, then an exception will occur. For example:
- using (Car carObj = new Car(), Animal animalObj = new Animal())
- {
- }
- // Here have two types, Car and Animal.So we will get a error;
- // Error Number: CS1044
- // Error Details: Cannot use more than one type in a for, using, fixed, or declaration statement
- You can use a nested using statement.
- using (Customer customer = new Customer())
- {
- using (Order order = customer.Order)
- {
- Console.WriteLine(order.ItemName);
- }
- }
- // or you can use flowing syntax (but it's not working for old c# version):
- using (Customer customer = new Customer())
- using (Order order = customer.Order)
- {
- Console.WriteLine(order.ItemName);
- }
- How does it work?
- Defines a boundary/Scope for the object. At the end of this boundary, it's called Dispose method automatically, which is written in resource class to dipose/release resource.
- When do we use it?
- For resource-intensive operations, object lifetime is important to dispose.
- Use using statement in database connection because if any error occurs, then the database connection will be closed automatically. There's no need to call connection Close() method explicity.
- Benefits:
- Reduces the code
- Example:
- using (Car carObj = new Car())
- {
- Console.WriteLine(carObjWithUsing.Name);
- }
- // you will get same result by try-catch-finally statement.
- Car carObj = new Car();
- try
- {
- Console.WriteLine(carObj.Name);
- }
- catch(Exception ex)
- {
- }
- finally
- {
- if(carObj != null)
- ((IDisposable)carObj).Dispose();
- }
- // From the above code, it's clear that using statement is reducing code.
- Simpler Syntax
- Example : see above in the code reduction example.
- Saves development time and effort.
- It ensures that Dispose is called even if an exception occurs within the using block.
- string connString = "Data Source=localhost;Integrated Security=True;Initial Catalog=Northwind;";
- using (SqlConnection conn = new SqlConnection(connString))
- {
- SqlCommand cmd = conn.CreateCommand();
- cmd.CommandText = "SELECT CustomerId, CompanyName FROM Customers";
- conn.Open();
- ....
- ....
- }
- // Here we dont need to write conn.Close(). It will call automatically.
- Disadvantages:
Within the using block, the object is read-only and cannot be reassigned or modified.
Last Word
I would appreciate any suggestions to help improve my articles.

Rikam PalkarPosted May 28, 2020, 9:03 AM
hey good explanation man
Mahabub RahamanPosted Nov 19, 2019, 2:17 AM
In one section, you do a good job of explaining the concept. Do more of that throughout.
Imamul KarimPosted Nov 19, 2019, 2:07 AM
Clear concepts.
Salahuddin AhmedPosted Nov 17, 2019, 8:00 AM
Great article
Sourav Kumar DasPosted Nov 4, 2019, 2:24 AM
Nice Article.