SqlConnection 在 Dispose 之前 要不要先 Close 呢?
我个人的习惯是会先叫 Close 再 叫 Dispose
但 Dispose 真的不会先帮我 Close 吗?
今天索性来反组译一下~
.methodfamilyhidebysigvirtualinstancevoid Dispose(bool disposing) cil managed{.maxstack 2.localsinit ([0] System.Data.ConnectionState state1)L_0000: ldarg.1L_0001: brfalse.s L_0027L_0003: ldarg.0L_0004: ldfld System.Data.ConnectionStateSystem.Data.SqlClient.SqlConnection::_objectStateL_0009: stloc.0L_000a: ldloc.0L_000b: switch (L_0020, L_001a)L_0018: br.s L_0020L_001a: ldarg.0L_001b: call instancevoidSystem.Data.SqlClient.SqlConnection::Close()L_0020: ldarg.0L_0021: ldnullL_0022: stfld System.Data.SqlClient.SqlConnectionStringSystem.Data.SqlClient.SqlConnection::_constrL_0027: ldarg.0L_0028: ldarg.1L_0029: call instancevoid [System]System.ComponentModel.Component::Dispose(bool)L_002e: ret}
所以答案是 Dispose 就好 ~
976

被折叠的 条评论
为什么被折叠?



