Introduction

This post explains how to utilize the using keyword.

using keyword

There are three major ways to use the using keyword,
Here we will discuss the Using Statement.

Using Statement

    • This Dispose method is working well now. But I have written only one: 'Console.WriteLine("Disposing");' We must write informative code to dispose.
      1. // adding new code with highlighted
      2. public class Car : IDisposable
      3. {
      4. public string Name { get; set; } = "Super Car";
      5. public void Dispose()
      6. {
      7. Console.WriteLine("Disposing");
      8. this.Name = null;
      9. }
      10. }
      11. class Program
      12. {
      13. static void Main(string[] args)
      14. {
      15. Car carObjWithUsing = new Car();
      16. using (carObjWithUsing)
      17. {
      18. Console.WriteLine(carObjWithUsing.Name);
      19. }
      20. // Name will be null here.
      21. Console.WriteLine(carObjWithUsing.Name);
      22. }
      23. }
      24. //Output
      25. Super Car
      26. 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:
      1. using (Car carObjWithUsing1 = new Car(), carObjWithUsing2 = new Car())
      2. {
      3. }
    • Note
      If you use more than one type within a single statement, then an exception will occur. For example:
      1. using (Car carObj = new Car(), Animal animalObj = new Animal())
      2. {
      3. }
      4. // Here have two types, Car and Animal.So we will get a error;
      5. // Error Number: CS1044
      6. // Error Details: Cannot use more than one type in a for, using, fixed, or declaration statement
    • You can use a nested using statement.
      1. using (Customer customer = new Customer())
      2. {
      3. using (Order order = customer.Order)
      4. {
      5. Console.WriteLine(order.ItemName);
      6. }
      7. }
      8. // or you can use flowing syntax (but it's not working for old c# version):
      9. using (Customer customer = new Customer())
      10. using (Order order = customer.Order)
      11. {
      12. Console.WriteLine(order.ItemName);
      13. }
  • 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:
        1. using (Car carObj = new Car())
        2. {
        3. Console.WriteLine(carObjWithUsing.Name);
        4. }
        5. // you will get same result by try-catch-finally statement.
        6. Car carObj = new Car();
        7. try
        8. {
        9. Console.WriteLine(carObj.Name);
        10. }
        11. catch(Exception ex)
        12. {
        13. }
        14. finally
        15. {
        16. if(carObj != null)
        17. ((IDisposable)carObj).Dispose();
        18. }
        19. // From the above code, it's clear that using statement is reducing code.
    • Simpler Syntax

Last Word

I would appreciate any suggestions to help improve my articles.