What is the use of 'Using' statement in C#?

The using statement in C# is used for automatic resource management and memory cleanup. It ensures that resources are properly disposed of when they are no longer needed, even if an exception occurs. The using statement works with objects that implement the IDisposable interface.

The main goal of the using statement is to manage resources and automatically release them when the code block completes execution. This is particularly useful for file operations, database connections, and other system resources that need explicit cleanup.

Syntax

Following is the syntax for the using statement −

using (ResourceType resource = new ResourceType()) {
   // use the resource
}
// resource is automatically disposed here

You can also declare multiple resources in a single using statement −

using (var resource1 = new Resource1(), resource2 = new Resource2()) {
   // use both resources
}

How It Works

The using statement is syntactic sugar for a try-finally block. When the using block completes (either normally or through an exception), the Dispose() method is automatically called on the resource.

Using Statement Lifecycle Resource Created Code Block Executed Dispose() Called Automatic cleanup even if exception occurs Equivalent to try-finally block try { /* use resource */ } finally { resource.Dispose(); }

Using Statement with Custom Resources

Example

using System;

class Demo {
    static void Main() {
        using (SystemResource res = new SystemResource()) {
            Console.WriteLine("A");
        }
        Console.WriteLine("B");
    }
}

class SystemResource : IDisposable {
    public void Dispose() {
        Console.WriteLine("C");
    }
}

The output of the above code is −

A
C
B

Using Statement with File Operations

Example

using System;
using System.IO;

class FileDemo {
    static void Main() {
        using (StringWriter writer = new StringWriter()) {
            writer.WriteLine("Hello World!");
            writer.WriteLine("Using statement demo");
            Console.WriteLine("Content: " + writer.ToString());
        }
        Console.WriteLine("StringWriter disposed automatically");
    }
}

The output of the above code is −

Content: Hello World!
Using statement demo

StringWriter disposed automatically

Multiple Resources in Using Statement

Example

using System;

class MultipleResourceDemo {
    static void Main() {
        using (var res1 = new SystemResource("Resource1"), 
               res2 = new SystemResource("Resource2")) {
            Console.WriteLine("Using both resources");
        }
        Console.WriteLine("Both resources disposed");
    }
}

class SystemResource : IDisposable {
    private string name;
    
    public SystemResource(string name) {
        this.name = name;
        Console.WriteLine(name + " created");
    }
    
    public void Dispose() {
        Console.WriteLine(name + " disposed");
    }
}

The output of the above code is −

Resource1 created
Resource2 created
Using both resources
Resource2 disposed
Resource1 disposed
Both resources disposed

Common Use Cases

  • File I/O operations − FileStream, StreamReader, StreamWriter

  • Database connections − SqlConnection, SqlCommand

  • Graphics resources − Bitmap, Graphics objects

  • Network resources − HttpClient, WebRequest

  • Threading resources − Mutex, Semaphore

Conclusion

The using statement in C# provides automatic resource management by ensuring that Dispose() is called when the code block completes. It simplifies resource cleanup and prevents memory leaks, making it essential for working with files, database connections, and other system resources that implement IDisposable.

Updated on: 2026-03-17T07:04:35+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements