C# using statement

Learn via video courses
Topics Covered

Overview

In C#, the using statement is used to define a scope within which one or more objects can be utilized, and it ensures that these objects are properly disposed of when the scope is exited. This is particularly useful when working with objects that implement the IDisposable interface, as it guarantees the invocation of their Dispose method, which is responsible for releasing resources and cleaning up after the object.

What is C# using statement?

The using keyword in C# serves two primary purposes, each contributing to better code organization and resource management. Firstly, the using directive simplifies namespace handling within your code. By including using directives at the beginning of a code file, you can effortlessly import namespaces. This streamlines your code by allowing you to reference types and methods from these namespaces directly throughout the file, enhancing code readability and maintainability. Secondly, the using statement, introduced in C# 8 and later, plays a crucial role in managing objects that implement the IDisposable interface. This becomes particularly significant when dealing with resource management, such as files and network connections. The using statement ensures that the Dispose method of these objects is called properly. Even if an exception interrupts the code execution, the "Dispose" method is still guaranteed to execute, preventing resources from being locked indefinitely. This feature greatly enhances the reliability of resource management in your code.

syntax for using statement

The using statement in C# is mainly utilised for automatic resource management and ensuring that IDisposable objects are correctly disposed of when they exit scope. The syntax for the using statement in C# is as follows:

  • using: Keyword to start the using statement.
  • ResourceType: The type of resource that must implement IDisposable.
  • resource: Variable to hold the resource instance.
  • = new ResourceType(): Initializes the resource.

What is the use of ‘Using’ statement in C#?

The using statement in C# serves two primary purposes:

  1. Automatic Resource Management: The most common use of the using statement is for automatic resource management. It ensures that resources, particularly objects that implement the IDisposable interface, are properly disposed of when they go out of scope. This is crucial for releasing resources like file handles, database connections, network sockets, or any resource that requires explicit cleanup.

Output

Explanation

  • We define a class called ResourceType that implements the IDisposable interface. This interface requires us to implement the Dispose method, which is responsible for releasing any resources held by the object.

  • In the Main method, we begin by displaying "Starting the program" to indicate the program's start.

  • Inside the using statement, we create an instance of ResourceType named resource. This is where the automatic resource management comes into play. When the using block is entered, the constructor of ResourceType is called, and when the block is exited, the Dispose method is called automatically, ensuring proper cleanup.

  • We call the DoSomething method on the resource object within the using block just to simulate some usage of the resource.

  • After the using block, we display "End of the program" to indicate the program's end.

  1. Alias for Namespaces: The using directive can also be used outside of using statements to create an alias for namespaces. This simplifies code by allowing you to use types from a namespace without specifying the full namespace name every time.

Output

Explanation

  • The code showcases the use of using directives to create aliases for namespaces in C#.
  • It defines a custom class CustomList for demonstration.
  • Aliases (MyGeneric and MySystem) are created for the System.Collections.Generic and System namespaces, respectively.
  • An instance of CustomList is created and filled with items.
  • The items are displayed using aliases for System.Console, enhancing code readability.
  • It generates a random number using the alias for System.Random and displays it.

Conclusion

  • using directive simplifies namespace handling within your code.
  • Used for automatic resource management.
  • Ensures proper disposal of objects implementing IDisposable.
  • Prevents resource leaks by automatically calling Dispose when out of scope.
  • using statement commonly used for file streams, database connections, and other resources.
  • It Enhances code readability by creating aliases for namespaces.
  • Crucial for robust resource handling and organized code