积极答复者
应该在什么时候释放引用对象?

问题
-
答案
-
using的好处是即使有异常代码也会被执行
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 JSHZP 2011年4月29日 3:49
全部回复
-
把代码的第一行和第二行换个位置估计问题就解决了。
另外,这里不推荐显式调用 Dispose 方法,而应该用 using 把创建 Bitmap 的代码包含起来,这是一种模式,就像您用 foreach 而不是用 while 然后 enumerator.MoveNext()。
有时候代码分析器会有些问题,特别是 CA2000,Microsoft.Performance。代码分析不是万能的,您有时候要自己想想对不对,如果分析的不对,就应该用 SuppressMessage 把错误的分析屏蔽掉。
Mark Zhou谢谢您!
一、我把第一二行互换位置,确实可以排除CA2000;
二、因为实际项目中无法采用上面的办法,我使用了using语句,确实也能解决CA2000!
很纳闷,显式调用Dispose方法会CA2000,而使用using隐式调用就OK,您可以告诉我其中的道理吗?
-
using的好处是即使有异常代码也会被执行
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 JSHZP 2011年4月29日 3:49