TAILIEUCHUNG - Inadvisable Destructor

Resource Management Sometimes it's inadvisable to release a resource in a destructor; some resources are just too valuable and too scarce to lie around unreleased for arbitrary lengths of time. | Resource Management Sometimes it s inadvisable to release a resource in a destructor some resources are just too valuable and too scarce to lie around unreleased for arbitrary lengths of time. Scarce resources need to be released and they need to be released as soon as possible. In these situations your only option is to release the resource yourself. A disposal method is a method that disposes of a resource. If a class has a disposal method you can call it explicitly and thereby control when the resource is released. Disposal Methods An example of a class that implements a disposal method is the TextReader class from the namespace. This class provides mechanisms to read characters from a sequential stream of input. TextReader contains a virtual method called Close which closes the stream. The StreamReader class which reads characters from a stream such as an open file and the StringReader class which reads characters from a string both derive from TextReader and both override the Close method. Here s an example that reads lines of text from a file by using the StreamReader class and then displays them on the screen TextReader reader new StreamReader filename string line while line null line The ReadLine method reads the next line of text from the stream into a string. The ReadLine method returns null if there is nothing left in the stream. It s important to call Close when you have finished with reader to release the file handle and associated resources. However there is a problem with this example it s not exception-safe. If the call to ReadLine or WriteLine throws an exception the call to Close will not happen it will be bypassed. If this happens often enough you will run out of file handles and be unable to open any more files. Exception-Safe Disposal One way to ensure that a disposal method such as Close is always called regardless of whether there is an exception is to call the disposal method inside a

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.