使用 ??运算符并处理空值
我从 SQL Server 2008 数据库返回一个标量值:
string reason = cmd.ExecuteScalar().ToString() ?? : "";
我想确保如果返回 null
,则 reason = ""
而不是 null。
我在这一行收到错误:
错误 3 无效的表达式术语“:”
如何解决此问题?
编辑:
感谢您对冒号的更改,现在我在同一行收到此异常:
string reason = cmd.ExecuteScalar().ToString() ?? "";
发生 System.NullReferenceException 消息 =“未将对象引用设置为对象的实例。”
I am returning a scalar value from a SQL Server 2008 database:
string reason = cmd.ExecuteScalar().ToString() ?? : "";
I want to make sure that if null
is returned, that reason = ""
and not null.
i am getting an error on this line:
Error 3 Invalid expression term ':'
How can this be fixed?
EDIT:
thank you for the changes on the colon, now i am getting this exception on the same line:
string reason = cmd.ExecuteScalar().ToString() ?? "";
System.NullReferenceException occurred Message="Object reference not set to an instance of an object."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
但是:这仍然会失败,因为如果
.ExecuteScalar()
返回NULL
,则已经导致空引用异常通过在该 NULL 值上调用.ToString()
......所以我猜
??
运算符在这里真的帮不了你......做“通常”舞蹈:Try this:
BUT: this will still fail, since if
.ExecuteScalar()
returns aNULL
, you're already causing a Null Reference Exception by calling.ToString()
on that NULL value......So I guess the
??
operator really doesn't help you here... do the "usual" dance:首先,使用 ?? 时不应该有 :操作员。
其次,为了在不出现错误的情况下执行您在此处尝试执行的操作,您需要以不同的方式执行此操作:
这将检查您的返回值是否为空,如果是,第二行会将原因设置为空字符串,否则它将使用您的返回值。
First, you shouldn't have the : when using the ?? operator.
Second, to do what you are trying to do here without getting an error, you need to do it differently:
This will check whether or not your returned value is null and if it is, the second line will set reason to a blank string, otherwise it will use your returned value.
由于
ExecuteScalar()
返回的object
可能为null
你不应该调用.ToString()
因为这可能会抛出和例外。这是有效的,因为
Convert.ToString()
会将null
转换为string.Empty
或者如果您必须使用
??
因为你真的很喜欢它:Since
ExecuteScalar()
returnsobject
that might benull
you should not call.ToString()
since that may throw and exception.This works because
Convert.ToString()
will convertnull
tostring.Empty
or if you must use
??
because you really like it:只要去掉冒号就可以了。
如需参考,请查看 MSDN 页面。
Just get rid of the colon.
For reference, check the MSDN page.
使用空合并运算符时,您不需要冒号:
正如其他人指出的那样,ToString() 无论如何都会导致抛出 NullReferenceExcpetion...所以您在这里不会获得任何东西。你最好将其分成多行:
When using the null-coalescing operator, you don't need the colon:
As others have pointed out though, ToString() would cause a NullReferenceExcpetion to be thrown anyway...so you don't gain anything here. You'd be much better off splitting this into multiple lines:
您对 感到困惑吗?条件运算符,其语法如下所示:
使用 ??空合并运算符,其语法如下:
所以,除此之外... 这是您真正想要的部分:
这会处理您的异常,其中
ToString( )
导致空引用异常。You're confusing the ? conditional operator, the syntax for which looks like this:
with the ?? null-coalesce operator whose syntax is as follows:
So, apart from all that... this is the part you really want:
This takes care of your exception where
ToString()
was causing a null-reference exception.只需使用
Just use