cool hit counter A plain understanding of Close and Dispose in C#_Intefrankly

A plain understanding of Close and Dispose in C#

Many classes in .net provide Close() and Dispose() methods, and I've always assumed that they were the same thing, that they were completely equivalent, and that it was sufficient to use one or the other anywhere, a realization that stems from the design advice on these two methods in the book NET Design Specifications: Conventions, Conventions, and Patterns, P239 and P240. By the literal reading of the book, Close is supposed to be designed to do the same thing as Dispose, a consideration to take care of the natural language, and indeed for some classes, saying Close is more in line with the user's understanding than saying Dispose (e.g. closing a connection, closing a stream), so this makes me think that Close exists as an alternative channel of use for Dispose, and that just one Dispose would have been enough, and in fact I haven't had any problems with using them both at random, so I haven't looked into it.

But today, while answering a Baidu Know question involving a discussion of these two methods, I consciously experimented with.

SqlConnection conn = new SqlConnection("linked string");

 conn. Close();//close first
 Console.WriteLine(conn. State);//State of the connection: closed

 Console.WriteLine(conn. ConnectionString);//at this point the connection string is still
 conn. Open();// Open again. normal

 conn. Dispose();// release first
 Console.WriteLine(conn. State);//State of the connection: closed

 Console.WriteLine(conn. ConnectionString);// connection string to null for string. Empty
 conn. Open();// Open again. throw an exception

Test results.

1. Both have closed the connection

2. After Close, the connection can be opened again; while after Dispose, the connection string is cleared and the connection cannot be opened again

So it seems that Close and Dispose are not exactly the same thing, which is a bit ruinous. Now it seems to be understood that Close is not responsible for destroying the object, but only for achieving a business "close" according to the function of the class, in this case, just changing the connection state (from connected → closed); while Dispose, as its mission, destroys the object and releases the resources. In other words, Close is only relevant to the business and Dispose is only relevant to the object. So the object that was closed can be "opened" and used again, while Dispose is a complete bust.

In summary, with respect to both of them, the following shallow conclusions are drawn.

1, Close is responsible for closing the business, Dispose is responsible for destroying the object. Dispose will take care of everything for Close, with the additional job of destroying the object, i.e. Dispose includes Close

2, when you have a clear need, do not mix the two

3. It doesn't make much sense to use the two together. Either close ~ so it can be used again, or destroy ~ no longer use

1、Several ways to get request in Spring and its threadsafe analysis
2、A Study of Hardware License Token Security
3、10 books that 97 of java learners have in their collection how many do you know
4、Bosch may halt its own production plans due to risky electric car battery business
5、Apple uses blockchain technology to do its thing blockchain to create and verify timestamps

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送