Difference between dispose and close in C #

Source: Internet
Author: User

This articleArticleIs reprinted, source: http://www.xici.net/b713781/d47980278.htm

When we develop C #CodeSome 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 we want to implement close and dispose, pay attention to the design pattern 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 (). Dispose () ", but for explicit implementation, dispose is not a member function of this class. The only call method is that you must first cast to idisposable. ("New (). Dispose () "cannot be compiled, but" (idisposable) New ()). Dispose () "can be compiled ). Therefore, it meets the design requirements: provide close (), hide dispose (), and implement the idisposable interface.

In. Net Framework, close () is designed to be public, and call the hidden dispose () in close (); dispose () to call another virtual dispose (bool) function. Therefore, if you inherit from this class, you must implement the 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. See http://msdn2.microsoft.com/en-us/library/ms227422.aspx. The document mentioned that although close () is virtual, it should not be override.

Note: This article is translated from our wonderful explanation of team manager Richard Wong and I hope it will help you. Thank you, Richard!

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.