C# 中的 null 简写?
有没有办法让这条线变短?
bool pass = d != null && d["k"] != null && (bool)d["k"];
注意:“k”实际上是“更长的id”; 我将其替换为使其在本文中更具可读性。 您的许多建议不检查 d 是否为空。
Is there a way to make this line shorter?
bool pass = d != null && d["k"] != null && (bool)d["k"];
Note: "k" is actually "a longer id"; I replaced it to make it more readable in this post. Many of your suggestions don't check whether d is null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
它已经很难读了,那为什么还要把它缩短呢?
It is already barely readable, so why make it any shorter?
尝试这个:
Try this:
我想问你为什么要缩短它。
较短的代码对任何人都没有好处。 这使得其他人阅读它变得更加困难。
就我个人而言,我会问“如何使这段代码更易于阅读”,答案是将其分成多行,并且每行只做一件事。
C# 中唯一可用的“空速记”是 null-coalescing运算符,它允许您为 null 对象定义默认值。 但对于您的代码,我建议以可读性为目标,而不是行大小。
I would question why you are trying to make it shorter.
Shorter code doesn't benefit anybody. It makes it far harder for someone else to read it.
Personally, I would be asking "how can I make this code easier to read", and the answer to that would be split it up into multiple lines, and only do 1 thing on each line.
The only 'null shorthand' available in C# is the null-coalescing operator, which allows you to define a default value for a null object. But for your code, I'd recommend aiming for readability, not line size.
是的,从中提取一个函数并将其命名为
doTheValuesInTheFooBarDictionaryPassTheRebarTest
。这更容易阅读。 当然,该方法仍将包含上面的行,但代码的读者(从这里开始直到时间结束)在浏览代码时将获得更愉快的体验。
通过缩短代码,您希望实现什么? 阅读速度更快? (大脑需要更长的时间来解析它!)。 打字速度更快? (你输入这些内容一次,然后修改或一遍又一遍地阅读它(一遍又一遍!))。 一般来说,更长、更具描述性的变量更好,如果你可以将它们包装在封装方法中,那么也可以这样做。
Yes, extract a function from it and call it something like
doTheValuesInTheFooBarDictionaryPassTheRebarTest
.That's much easier to read. Of course, the method will still contain your line above, but the reader of the code (from here 'till the end of time) will have a MUCH more pleasant experience when skimming the code.
By making code shorter, what are you hoping to achieve? Quicker to read? (it'll take longer for the brain to parse it!). Quicker to type? (you type this stuff once and either modify or read it again and again (and again!)). Generally, longer, more descriptive variables are better, and if you can wrap them in an encapsulating method, then do that too.
您可以通过在构造函数中初始化 d 来摆脱第一个表达式。 我建议您使用通用字典,即
这样您就不必强制转换该值。 并使用 ContainsKey 方法而不是直接访问值。 所以你的最终表达
You can get rid of the first expression by initializing d in the constructor. I recommend you to use generic dictionary i.e.
That way you don't have to cast the value. And use the ContainsKey method as opposed to directly accessing the value. So your final expression
null 条件运算符和 null 合并运算符的组合应在此处起作用:
澄清:
d
为 null,则d?["k"]
为 nulld?["k"]
为空,d?["k"] ?? false
是false
(装箱为object
)另一种方法是在空合并运算符之前进行强制转换,但要强制转换为可空类型:
请注意,空条件运算符是在 C# 6 中引入的。
A combination of the null-conditional and null-coalescing operators should work here:
To clarify:
d
is null,d?["k"]
is nulld?["k"]
is null,d?["k"] ?? false
isfalse
(boxed asobject
)An alternative would be to cast before the null-coalescing operator, but to cast to a nullable type:
Note that the null-conditional operator was introduced in C# 6.
我不会尝试进一步缩短它,我会尝试给它一个好听的名字。 编写扩展方法或修改类(如果您有权访问代码)对我来说听起来不错。
并像这样使用它。
I would not try to shorten it further, I would try to give it a nice name. Writing an extension method or modifying the class (if you have access to the code) sounds good to me.
And use it like that.
是的。 使 d 成为强类型(例如,使用 Dictionary<> 而不是 Hashtable)
如果我们知道 d 的类型,将会有所帮助!
Yes. Make d strongly typed (e.g. use Dictionary<> instead of Hashtable)
It would help if we knew the type of d!
我会质疑为什么你要把 d["k"] 转换为 bool 。 要么它已经是一个布尔值,要么它应该与某个东西进行比较以获得布尔值。 这会让事情变得更清楚。
无论如何,我想这可能有效:
I would question why you are casting d["k"] to a bool. Either it is already a bool or it should be compared to something to get a bool. This would make things clearer.
Anyway, I suppose this might work:
这不是更短,而是更可靠(也许更高效):
我建议将其移至函数中。
This is not shorter, but more reliable (and perhaps a bit more efficient):
I would recommend moving it into a function.
您可以将其替换为接受字典和键的函数调用......
You could replace it with a function call that takes in a dictionary and a key...
我更愿意看到这样的东西:
或者如果想为
System.Object
编写一个方便的扩展方法,使其看起来像这样:我发现这比原始版本更容易理解。
I would much prefer to see something like this:
or if wanted to write a handy extension method for
System.Object
that would make it look like this:I find this much more understandable than the original version.
在 c# 6 及以上版本中,您可以使用
如果 d["k"] 的编译时类型是 bool? 你可以省略演员阵容
In c# 6 and above you can use
If the compile time type of d["k"] is bool? you can omit the cast