using Statement in C#
Written by Thong D. Nguyen Friday, 02 July 2010 09:53
Blog - Programming
C#, though the .NET Framework common language runtime (CLR) automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.
The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.
A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.
The object can be declared in the using statement, as shown above, or before the using statement, like this:
|
Multiple objects can be used in with a using statement, but they must be declared inside the using statement, like this:
using (Font font3 = new Font("Arial", 10.0f),
|
The following sample shows how a user-defined class can implement its own Dispose behavior. Note that your type must inherit from IDisposable.
using System; |
| < Prev | Next > |
|---|
Idioms
- There's no rose without a thorn (Hồng nào mà chẳng có gai)
- Every man has his faults (Nhân vô thập toàn)
Who's online
Location
38.107.179.217
38.107.179.217Search Bot
unknown unkno

OS
PHP
MySQL
Time
Caching
GZIP
Members
Content
Web Links





Today
Yesterday
This week
Last week
This month
Last month
All days
