对属性或调用方法使用空合并

发布于 2024-10-30 06:09:30 字数 696 浏览 1 评论 0原文

可以使用 ??在这样的情况下操作:

string str = collection["NoRepeate"] ?? null; // Will not compile 
                          //because collection["NoRepeate"] is object

这里的问题是不可能将collection["NoRepeate"](即object)分配给str并且 collection["NoRepeate"].ToString() 当其值为 null 时抛出异常。

我现在使用条件运算符 ?:

str = collection["NoRepeate"].HasValue ? collection["NoRepeate"].ToString() : null

但问题在于重复常量字符串。

It is possible to use the ?? operation in a situation such this:

string str = collection["NoRepeate"] ?? null; // Will not compile 
                          //because collection["NoRepeate"] is object

The problem here is that it is not possible to assign collection["NoRepeate"] which is object to str and collection["NoRepeate"].ToString() throws exception when its value is null.

I'm using now conditional operator ?::

str = collection["NoRepeate"].HasValue ? collection["NoRepeate"].ToString() : null

But the problem is in repeating the constant string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

意犹 2024-11-06 06:09:30

我同意你的观点,这有点令人烦恼,这不能通过单一声明来完成。空合并运算符在这里没有帮助。

据我所知,最短的需要两个陈述。

object obj = collection["NoRepeate"];
string str = obj == null ? null : obj.ToString();

I agree with you that it is a bit vexing that this cannot be done in a single statement. The null coalescing operator does not help here.

The shortest I am aware of requires two statements.

object obj = collection["NoRepeate"];
string str = obj == null ? null : obj.ToString();
云雾 2024-11-06 06:09:30

您可以执行以下操作:

string str = (string) collection["NoRepeate"] ?? null;

但这假设该对象实际上是一个字符串。如果不是,您将收到运行时错误。其他解决方案更加强大。

不过,让这个时间尽可能短并没有什么实际意义。你应该让你的代码可读,而不是“我能写多短?”

You can do the following:

string str = (string) collection["NoRepeate"] ?? null;

This assumes the object is actually a string though. If it's not, you're going to get a run time error. The other solutions are more robust.

There is no real point in getting this to be as short as possible though. You should make your code readable, not "How short can I write this?"

触ぅ动初心 2024-11-06 06:09:30

我的解决方案是:

var field = "NoRepeate";

var str = collection[field].HasValue ? collection[field].ToString() : null;

当然,如果你没有原始痴迷,你可以添加一个集合类的方法来执行此操作。否则你可能不得不坚持使用扩展方法。

My solution is:

var field = "NoRepeate";

var str = collection[field].HasValue ? collection[field].ToString() : null;

Of course, if you don't have primitive obsessions you can add a method to the collection class to do this this. Else you'll probably have to stick with an extension method.

静若繁花 2024-11-06 06:09:30

从集合返回的对象实际上是 Nullable吗?否则,您可能需要显式检查 null:

var item = collection["NoRepeate"];
string str = (item == null) ? null : item.ToString();

Is the object returned from the collection actually a Nullable<object>? Otherwise, you probably want to explicitly check for null:

var item = collection["NoRepeate"];
string str = (item == null) ? null : item.ToString();
随遇而安 2024-11-06 06:09:30

是的,如果返回值为 null,则很有可能。如果返回值为“”则否。我一直使用它为空值分配默认值。

Yes that's very possible, if the returned value is null. If the returned value is "" then no. I use it all the time for null values to assign a default.

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