When we develop C # Code Some classes provide close () and some classes provide dispose (). What is the difference between dispose and close?
First, dispose and close should be basically the same. Close is designed for developers who are not familiar with dispose. Because basically all developers know what close is (especially for developers with a C ++ background ).
But when writing code, if you want to implement close and dispose, pay attention to the design mode of close and dispose .. Some Classes of net only provide close, and are derived from idisposable, And the dispose method is hidden. Do you think it is hard to understand?
For these classes, the key lies in their explicit implementation of idisposable. For implicit implementation, you only need to call "New
A (). Dispose () ", but for explicit implementation, dispose is not a member function of this class. The only way to call is to cast
Idisposable. ("New A (). Dispose ()" cannot be compiled, but "(idisposable) New
A (). Dispose () "can be compiled ). Therefore, this meets the design requirements: provide close (), hide dispose (), and implement
Idisposable interface.
In the. NET Framework, close () is designed as public, and call is hidden dispose () in close ();
Dispose () calls another virtual dispose (bool) function. Therefore, if you inherit from this class, you must implement dispose.
(Bool) method.
When the caller calls close (), it will call the overloaded dispose (bool) method to release the resource.
See http://blogs.msdn.com/brada/archive/2003/07/06/50127.aspx
Note:
1. Close () should not be defined as virtual. For this design
Pattern, close () is only used to call the hidden dispose (). You should not change the close behavior. For this problem,
System. Io. Stream also has design problems. The reason for the problem is to meet backward compatibility requirements. SeeHttp://msdn2.microsoft.com/en-us/library/ms227422.aspx.Although close () is virtual, it should not be override.
The Demo code is as follows: 1 Using System;
2
3 Namespace Consoleapplication
4 {
5 Abstract Class Mystream: idisposable
6 {
7 Public Mystream ()
8 {
9 M_unmanagedresource = Marshal. alloccotaskmem ( 100 );
10 M_bitmap = New Bitmap ( 50 , 50 );
11 }
12
13 # Region Idisposable members
14 Void Idisposable. Dispose ()
15 {
16 Dispose ( True );
17 GC. suppressfinalize ( This );
18 }
19
20 Protected Virtual Void Dispose ( Bool Isdisposing)
21 {
22 If ( ! M_disposed)
23 {
24 If (Isdisposing)
25 {
26 M_bitmap.dispose ();
27 }
28 Marshal. freecotaskmem (m_unmanagedresource );
29 M_disposed = True ;
30 }
31 }
32
33 Public Void Close ()
34 {
35 (Idisposable) This ). Dispose ();
36 }
37
38 ~ Mystream ()
39 {
40 Dispose ( False );
41 }
42
43 Private Intptr m_unmanagedresource; // Unmanaged Resource
44 Private Bitmap m_bitmap; // Idisposable managed resources
45 Private Bool M_disposed;
46
47 # Endregion
48 }
49
50 Class Myderivedstream: mystream
51 {
52 Public Myderivedstream ()
53 {
54 M_anothermemory = Marshal. alloccotaskmem ( 20 );
55 M_anotherimage = New Bitmap ( 24 , 24 );
56 }
57
58 Protected Override Void Dispose ( Bool Isdisposing)
59 {
60 If ( ! M_disposed)
61 {
62 If (Isdisposing)
63 {
64 M_anotherimage.dispose ();
65 }
66
67 Marshal. freecotaskmem (m_anothermemory );
68 Base . Dispose (isdisposing );
69 M_disposed = True ;
70 }
71 }
72
73 Public Static Void Main ( String [] ARGs)
74 {
75 Mystream astream = New Myderivedstream ();
76
77 Astream. Close (); // Allowed
78 // Astream. Dispose (); // Cannot compile
79
80 (Idisposable) astream). Dispose (); // Allowed
81
82 //
83 // This one works as well, because newstream callthe explicit implemented
84 // Idisposable. Dispose Method
85 //
86 Using (Mystream newstream = New Myderivedstream ())
87 {
88 //
89 // Do something
90 //
91 }
92 }
93
94 Private Intptr m_anothermemory;
95 Private Bitmap m_anotherimage;
96 Private Bool M_disposed;
97 }
98 }
99