Tải bản đầy đủ (.pdf) (2 trang)

Chapter 13 Quick Reference

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.69 KB, 2 trang )



Chapter 13 Quick Reference
To Do this
Write a destructor
Write a method whose name is the same as the name of the class
and is prefixed with a tilde (~). The method must not have an access
modifier (such as public) and cannot have any parameters or return
a value. For example:
class Example
{
~Example()
{
...
}
}
Call a destructor
You can't call a destructor. Only the garbage collector can call a
destructor.
Force garbage
collection
Call System.GC.Collect.
Release a resource at
a known point in
time
Write a disposal method (a method that disposes of a resource) and
call it explicitly from the program. For example:
class TextReader
{
...
public virtual void Close()


{
...
}
}

class Example
{
void Use()
{
TextReader reader = ...;
// use reader
reader.Close();
}
}
To Do this
Release a resource at
a known point in
time in an
exception-safe
manner
Release the resource with a using statement. For example:
class TextReader : IDisposable
{
...
public virtual void Dispose()
{
// calls Close
}
public virtual void Close()
{

...
}
}

class Example
{
void Use()
{
using (TextReader reader = ...)
{
// use reader
}
}
}



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×