是否有理由在 C# 中使用clausule 检查多个内部的 null ?
是否有理由在上次使用时检查 null ?在我看来,它很可能不需要?
using (var connection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand(commandString, connection))
{
using (var reader = command.ExecuteReader())
{
if (reader != null) {
// Use the reader
}
}
}
}
编辑:
如果我像这样使用它,我应该在使用中关闭 reader.Close() 和 connection.Close() 吗:
using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetailsDZP))
using (var sqlWrite = new SqlCommand(preparedCommand, varConnection)) {
while (sqlWrite.Read()) {
//something to do.
}
sqlWrite.Close();
varConnection.Close();
}
public static SqlConnection sqlConnectOneTime(string varSqlConnectionDetails) {
SqlConnection sqlConnection = new SqlConnection(varSqlConnectionDetails);
sqlConnect(sqlConnection);
if (sqlConnection.State == ConnectionState.Open) {
return sqlConnection;
}
return null;
}
在下面的示例中是否需要使用 close 或者我可以跳过这两个? sqlWrite.Close(); varConnection.Close();
谨此致意,
疯子
Is there a reason to check for null in the last using? Seems to me like it's most likely not gonna be needed?
using (var connection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand(commandString, connection))
{
using (var reader = command.ExecuteReader())
{
if (reader != null) {
// Use the reader
}
}
}
}
EDIT:
Should i close reader.Close() and connection.Close() inside the using if i use it like that:
using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetailsDZP))
using (var sqlWrite = new SqlCommand(preparedCommand, varConnection)) {
while (sqlWrite.Read()) {
//something to do.
}
sqlWrite.Close();
varConnection.Close();
}
public static SqlConnection sqlConnectOneTime(string varSqlConnectionDetails) {
SqlConnection sqlConnection = new SqlConnection(varSqlConnectionDetails);
sqlConnect(sqlConnection);
if (sqlConnection.State == ConnectionState.Open) {
return sqlConnection;
}
return null;
}
Is using of close necessary in following example or i can skip those two?
sqlWrite.Close();
varConnection.Close();
With regards,
MadBoy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,这没有必要。
但这是从
ExecuteReader
的定义得出的,与using子句无关。ExecuteReader
要么返回一个(非空)DataReader 对象,要么抛出异常。if
语句中的表达式将始终为 true(如果达到)。通过省略
if
和所有多余的大括号对,可以使代码更容易阅读:No, this is not necessary.
But that follows from the definition of
ExecuteReader
, it is not related to the using clause.ExecuteReader
either returns a (non-null) DataReader object or it throws an exception.The expresion in the
if
statement will always be true (if it is reached).And by leaving out the
if
and all the superfluous brace-pairs you can make this much easier to read:不,你为什么要这么做? 文档并不表明它可能为空。如果它为 null,你会怎么做?再试一次?
No. Why would you do that? The documentation doesn't suggest that it might be null. What would you do if it was null, anyway? Try it again?
由于您专门使用 SqlConnection 和 SqlCommand - 不,没有理由进行此检查。
然而,ADO 接口使您可以插入任何其他数据库提供程序并通过 ADO 基类(DbConnection、DbCommand 等)使用它们。似乎某些提供商有时会返回
null
(MySQL也许?),这可能就是程序员插入这个的原因。或者只是因为 ReSharper 在此发出警告?这些可能是造成这种情况的原因,尽管如果使用的提供商遵守合同则没有必要。Since you are specifically using SqlConnection and SqlCommand - no, there is no reason for this check.
However, the ADO interfaces are so that you can plug in any other DB provider and use them via the ADO base classes (DbConnection, DbCommand etc). It seems that some providers are returning
null
sometimes (MySQL maybe?), which may be why the programmer inserted this. Or just because ReSharper issues a warning here? These may be reasons for this, even though it is not necessary if the providers used follow the contract.using 语句不会为您检查 null,因此如果 command.ExecuteReader() 可以返回 null,您必须显式检查它,就像在代码片段中一样。
编辑:看起来在这种特殊情况下 ExecuteReader() 现在应该返回 null,因此您可以避免它。请记住,一般情况下您都需要它。
the using statements are not going to check for null for you, so if command.ExecuteReader() can return null you have to check for it explicitly, just like in your code snippet.
EDIT: Looks like in this particular case ExecuteReader() should now return null, so you can avoid it. Please remember that you need it in general case though.
似乎空检查是值得的,只是为了避免对象为空的可能性很小。看看我对如何我可以在循环中修改队列集合吗?
在示例中,
Queue.Dequeue()
永远不会返回 null,但它确实返回了 null。这是一个极端的情况,但我不明白为什么你不想避免未处理的异常,特别是如果它像if (object != null)
For Henk 一样简单(你可以运行代码自己并得到类似的结果):
alt text http://www.ccswe.com/temp/Untitled .png
不管怎样,我只是简单地陈述我的观点,仅仅因为文档说它会做一件事并不意味着它总是会做。不知道为什么我仅仅因为有人有不同的意见而得到了否决:-)
编辑:愚蠢的工具提示,无论哪种方式,你都可以看到处理后的值是 9998065,遇到的空值是 2264。如果有我的示例代码有根本性的错误,我有兴趣听到它。我现在要退出这个话题了。
Seems like the null check is worth it simply to avoid that slim chance that the object is null. Take a look at my answer to How can I modify a queue collection in a loop?
In the example
Queue.Dequeue()
should never return a null but it does. It's an extreme case but I don't see why you wouldn't want to avoid an unhandled exception, especially if it's as easy asif (object != null)
For Henk (you can run the code yourself and get similar results):
alt text http://www.ccswe.com/temp/Untitled.png
Either way I was simply stating my opinion that just because the documentation says it will do one thing doesn't mean it always will. Not sure why I got the downvote simply because someone has a different opinion :-)
Edit: Stupid tool tip, either way you can see the processed is 9998065 and the null values encountered is 2264. If there is something fundamentally wrong with my example code I'd be interested in hearing it. I'm gonna back away from this thread now.
在这种情况下,您不应检查 reader 是否为 null。
但是您可以使用 Code Contracts 库并使用 Contract.Assume (或 Contract .Assert)来写下你对代码的假设。在这种情况下,您可以使用静态分析工具并轻松地从发布版本中删除此检查。
In this case you should not check reader for null.
But you may use Code Contracts library and use Contract.Assume (or Contract.Assert) to write your assumptions about code. In this case you can use tools for static analysis and easily remove this checks from your release builds.