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

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.