Simple understanding of close () and dispose () in C #

Source: Internet
Author: User

. Many classes in. Net provide the close () and dispose () methods. I have always thought they are the same thing. They are completely equivalent. You can use them anywhere, this awareness is derived from the design suggestions of p239 and p240 in the book "net design specifications: Conventions, idioms and patterns. According to the literal understanding in the book, close should be designed as the same function as dispose, to take care of a consideration of natural language, indeed for some classes, close is more in line with the user's understanding (such as closing the connection and closing the stream) than dispose, so it makes me feel that close is another use channel of dispose, originally, only one dispose is enough. In fact, I didn't go into any problems because I used them at will.

However, when I answered a question from Baidu today, I consciously tried to discuss the two methods:

Sqlconnection conn = new sqlconnection ("connection string"); Conn. open (); Conn. close (); // close the console first. writeline (Conn. state); // connection status: Disabled

Console. writeline (conn. connectionstring); // The connection string is still in conn. open (); // open again. Normal conn. Dispose (); // release first
Console. writeline (conn. State); // connection status: Disabled

Console. writeline (conn. connectionstring); // set the connection string to be null, Which is string. emptyconn. open (); // open it again. Throw an exception

Test results:

1. The connection is closed.

2. After close, the connection can be opened again. After dispose, the connection string is cleared and the connection cannot be opened again.

In this case, close and dispose are not exactly the same thing, and they are a little ruined. Now it seems that close is not responsible for destroying objects. It is only based on the functions of the class to implement a "close" business. In this example, it only changes the connection status (from connection to close), while dispose, like its mission, destroys objects and releases resources. In other words, close is only related to business, and dispose is only related to objects. Therefore, the closed object can be "opened" and used again, while dispose is used to completely stop the dish.

To sum up, the two come to the simple conclusion:

1. Close is responsible for closing the business, and dispose is responsible for destroying objects. Dispose will be responsible for all the close transactions, as well as the destruction of objects, that is, dispose contains close

2. When you have specific requirements, do not mix them.

3. connecting the two is meaningless. Either close ~ For reuse, or destroy ~ No longer used

Related Article

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.