使用 ??运算符并处理空值

发布于 2024-11-23 23:39:21 字数 515 浏览 0 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

迷离° 2024-11-30 23:39:21

试试这个:

string reason = cmd.ExecuteScalar().ToString() ?? "";

但是:这仍然会失败,因为如果 .ExecuteScalar() 返回 NULL,则已经导致空引用异常通过在该 NULL 值上调用 .ToString() ......

所以我猜 ?? 运算符在这里真的帮不了你......做“通常”舞蹈:

object result = cmd.ExecuteScalar();

if(result != null)
{  
   reason = result.ToString();
}
else
{
   reason = "(NULL value returned)";
}

Try this:

string reason = cmd.ExecuteScalar().ToString() ?? "";

BUT: this will still fail, since if .ExecuteScalar() returns a NULL, 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:

object result = cmd.ExecuteScalar();

if(result != null)
{  
   reason = result.ToString();
}
else
{
   reason = "(NULL value returned)";
}
就此别过 2024-11-30 23:39:21

首先,使用 ?? 时不应该有 :操作员。

其次,为了在不出现错误的情况下执行您在此处尝试执行的操作,您需要以不同的方式执行此操作:

object objReason = cmd.ExecuteScalar();
string reason = objReason == null ? "" : objReason.ToString();

这将检查您的返回值是否为空,如果是,第二行会将原因设置为空字符串,否则它将使用您的返回值。

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:

object objReason = cmd.ExecuteScalar();
string reason = objReason == null ? "" : objReason.ToString();

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.

深者入戏 2024-11-30 23:39:21

由于 ExecuteScalar() 返回的 object 可能为 null 你不应该调用 .ToString() 因为这可能会抛出和例外。

string reason = Convert.ToString(cmd.ExecuteScalar());

这是有效的,因为 Convert.ToString() 会将 null 转换为 string.Empty


或者如果您必须使用 ??因为你真的很喜欢它:

(cmd.ExecuteScalar() ?? (object)"").ToString();

Since ExecuteScalar() returns object that might be null you should not call .ToString() since that may throw and exception.

string reason = Convert.ToString(cmd.ExecuteScalar());

This works because Convert.ToString() will convert null to string.Empty


or if you must use ?? because you really like it:

(cmd.ExecuteScalar() ?? (object)"").ToString();
奶气 2024-11-30 23:39:21

只要去掉冒号就可以了。

string reason = cmd.ExecuteScalar().ToString() ?? "";

如需参考,请查看 MSDN 页面

Just get rid of the colon.

string reason = cmd.ExecuteScalar().ToString() ?? "";

For reference, check the MSDN page.

救星 2024-11-30 23:39:21

使用空合并运算符时,您不需要冒号:

string reason = cmd.ExecuteScalar().ToString() ?? "";

正如其他人指出的那样,ToString() 无论如何都会导致抛出 NullReferenceExcpetion...所以您在这里不会获得任何东西。你最好将其分成多行:

var result = cmd.ExecuteScalar();
string reason = result == null ? "" : result.ToString();

When using the null-coalescing operator, you don't need the colon:

string reason = cmd.ExecuteScalar().ToString() ?? "";

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:

var result = cmd.ExecuteScalar();
string reason = result == null ? "" : result.ToString();
守望孤独 2024-11-30 23:39:21

您对 感到困惑吗?条件运算符,其语法如下所示:

String x = condition ? valueIfConditionIsTrue : valueIfConditionIsFalse;

使用 ??空合并运算符,其语法如下:

String x = possiblyNull ?? valueIfPossiblyNullIsNull;

所以,除此之外... 这是您真正想要的部分

String reason = (cmd.ExecuteScalar() ?? "").ToString();

这会处理您的异常,其中 ToString( ) 导致空引用异常。

You're confusing the ? conditional operator, the syntax for which looks like this:

String x = condition ? valueIfConditionIsTrue : valueIfConditionIsFalse;

with the ?? null-coalesce operator whose syntax is as follows:

String x = possiblyNull ?? valueIfPossiblyNullIsNull;

So, apart from all that... this is the part you really want:

String reason = (cmd.ExecuteScalar() ?? "").ToString();

This takes care of your exception where ToString() was causing a null-reference exception.

恰似旧人归 2024-11-30 23:39:21

只需使用

string reason = cmd.ExecuteScalar() ??  "";

Just use

string reason = cmd.ExecuteScalar() ??  "";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文