Cisco CloudCenter: Get the Hybrid IT Advantage

Using Statement

Definition - What does Using Statement mean?

A using statement, in C#, is a statement that contains the "using" keyword and provides the syntax to specify the scope of the use of a resource object.

The using statement is useful for objects whose lifetimes are within the method or block in which they are created. This statement reduces the code in which resource-intensive operations are performed by providing simpler syntax and eliminating the need for multiple calls to release the resources at the end of their scope. Because it simplifies the code necessary for creating, cleaning up and handling an error while using the resource object, the using statement saves development time and effort.

Techopedia explains Using Statement

The .NET framework is designed with an automatic memory management feature by which memory allocated to an object is released in non-deterministic mode whenever the Common Language Runtime decides to perform garbage collection (GC). However, unmanaged resources like file handles, network connections, etc., have to be released as quickly as possible. The using statement allows the developer to specify the instant at which such limited resources are to be released.

The using statement takes one parameter, a resource that is represented by a class or struct that implements the System.IDisposable interface. The only method that needs to be implemented through the interface is "Dispose," which can include the code necessary for cleaning up the resource. If this method is not called, the GC takes care of the disposal automatically.
For example, the using statement can be used to open a file stream by creating an object of System.IO.FileStream type, which can be used within the scope of the code block of the using statement.

The C# compiler translates the code for the using statement into three sections in Microsoft Intermediate Language:
  • Acquisition of the resource within the section for memory allocation of the resource
  • Use of the resource enclosed within a "try" block
  • Disposal of the resource by releasing it (calling the "Dispose" method of the resource object) within the "finally" block
The using statement does not allow the resource object to be modified or reassigned within the block in which it is used.
This definition was written in the context of C#
Share this:

Connect with us

Email Newsletter

Join thousands of others with our weekly newsletter

The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.