在 C# 中使用可空类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/259480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using nullable types in C#
提问by Martin Brown
I'm just interested in people's opinions. When using nullable types in C# what is the best practice way to test for null:
我只是对人们的意见感兴趣。在 C# 中使用可空类型时,测试空值的最佳实践方法是什么:
bool isNull = (i == null);
or
或者
bool isNull = !i.HasValue;
Also when assigning to a non-null type is this:
分配给非空类型时也是这样:
long? i = 1;
long j = (long)i;
better than:
优于:
long? i = 1;
long j = i.Value;
采纳答案by mackenir
Use the forms that were specially implemented for you by the C# team. If anyone objects, tell them Anders said it was okay.
使用 C# 团队专门为您实现的表单。如果有人反对,告诉他们安德斯说没关系。
What I'm saying, flippantly, is that a lot of work went into integrating nullable types into c# to give you a good programming experience.
我要说的是,为了给您提供良好的编程体验,在将可为空类型集成到 c# 方面做了大量工作。
Note that in terms of performance, both forms compile down to the same IL, ie:
请注意,在性能方面,两种形式都编译为相同的 IL,即:
int? i = 1;
bool isINull = i == null;
int j = (int)i;
Ends up like this after the C# compiler has got to it:
在 C# 编译器完成之后,结果是这样的:
int? i = 1;
bool isINull = !i.HasValue;
int j = i.Value;
回答by philsquared
I would always use the (i==null) form. It expresses what you are doing.
我总是使用 (i==null) 形式。它表达了你在做什么。
WRT the second question, I think either form is fine. However I'd always check it against null first and take appropriate action - perhaps wrapping that check and action up in a helper method (often it just sets a default value).
WRT第二个问题,我认为任何一种形式都可以。但是,我总是首先针对 null 对其进行检查并采取适当的措施 - 可能将该检查和操作包装在一个辅助方法中(通常它只是设置一个默认值)。
回答by Omer van Kloeten
They're both the same, but I would use the former version on both, since it's more common in the language: comparison to null and casting to a type.
它们都是相同的,但我会在两者上使用前一个版本,因为它在语言中更常见:与 null 比较和转换为类型。
回答by John
I usually tend to lean towards the first option in both scenarios, since it's more 'primitive' oriented opposed to object oriented (which was really what we were going for), but it really doesn't matter that much
在这两种情况下,我通常倾向于倾向于第一个选项,因为它更面向“原始”而不是面向对象(这正是我们想要的),但它真的没有那么重要
回答by Seiti
I would use this:
我会用这个:
long? i = 1;
...some code...
long j = i ?? 0;
That means, if iis null, than 0 will be assigned.
这意味着,如果i为null,则将分配 0 。
回答by Marc Bollinger
I haven't used Nullable Types in practice, but for the second, I'd actually suggest using j.GetValueOrDefault(). The documentation suggests that the latter would actually throw an InvalidOperationException in the event of a null value. Depending on the internal implementation of the explict cast operator for long?, the former might, too. I'd stick with GetValueOrDefault and treat the null/default case appropriately.
我在实践中没有使用过可空类型,但对于第二个,我实际上建议使用 j.GetValueOrDefault()。文档表明,后者实际上会在出现空值时抛出 InvalidOperationException。取决于显式转换运算符 long? 的内部实现,前者也可能。我会坚持使用 GetValueOrDefault 并适当地处理空/默认情况。
回答by Fry
I tend to use the first on both, because as it needs to be supported later in its life-cycle, these seem easier to understand what the intent of the original writer.
我倾向于在两者上使用第一个,因为因为它需要在其生命周期的后期得到支持,所以这些似乎更容易理解原始作者的意图。
回答by AdamSane
Opened up Reflector. HasValue is a lookup on a boolean flag which is set when the value is changed. So in terms of cycles a lookup is going to be faster then compare.
打开反射器。HasValue 是对布尔标志的查找,该标志在值更改时设置。因此,就周期而言,查找将比比较更快。
public Nullable(T value)
{
this.value = value;
this.hasValue = true;
}
private bool hasValue;
internal T value;
public bool HasValue
{
get
{
return this.hasValue;
}
}