Đang chuẩn bị liên kết để tải về tài liệu:
Apress Introducing Dot Net 4 With Visual Studio_7
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'apress introducing dot net 4 with visual studio_7', công nghệ thông tin, hệ điều hành phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | CHAPTER 13 IN SEARCH OF C CANONICAL FORMS Now can you think of what happens if the client of your object forgets to call Dispose or doesn t use a using statement Clearly there is the chance that you will leak the resource. And that s why the Win32Heap example type needs to also implement a finalizer as I describe in the next section. Note In the previous examples I have not considered what would happen if multiple threads were to call Dispose concurrently. Although the situation seems diabolical you must plan for the worst if you re a developer of library code that unknown clients will consume. Does the Object Need a Finalizer A finalizer is a method that you can implement on your class and that is called prior to the GC cleaning up your unused object from the heap. Let s get one important concept clear up front Finalizers are not destructors nor should you view them as destructors. Destructors usually are associated with deterministic destruction of objects. Finalizers are associated with nondeterministic destruction of objects. Unfortunately much of the confusion between finalizers and destructors comes from the fact that the C language designers chose to map finalizers into the C destructor syntax which is identical to the C destructor syntax. In fact you ll find that it s impossible to overload Object.Finalize explicitly in C . You overload it implicitly by using the destructor syntax that you re used to if you come from the C world. The only good thing that comes from C implementing finalizers this way is that you never have to worry about calling the base class finalizer from derived classes. The compiler does that for you. Most of the time when your object needs some sort of cleanup code for example an object that abstracts a file in the file system it needs to happen deterministically for example when manipulating unmanaged resources. In other words it needs to happen explicitly when the user is finished with the object and not when the GC finally gets .