C# 类型之间有什么区别吗?和可空<类型>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56518/
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
Is there any difference between type? and Nullable<type>?
提问by MojoFilter
In C# are the nullable primitive types (i.e. bool?
) just aliases for their corresponding Nullable<T>
type or is there a difference between the two?
在 C# 中,可为空的原始类型(即bool?
)只是它们对应Nullable<T>
类型的别名,还是两者之间有区别?
采纳答案by Steve Morgan
回答by Curt Hagenlocher
A Nullable<T>
is a structure consisting of a T and a bit flag indicating whether or not the T is valid. A Nullable<bool>
has three possible values: true, false and null.
ANullable<T>
是由 T 和指示 T 是否有效的位标志组成的结构。ANullable<bool>
具有三个可能的值:true、false 和 null。
Edit:Ah, I missed the fact that the question mark after "bool" was actually part of the type name and not an indicator that you were asking a question :). The answer to your question, then, is "yes, the C# bool?
is just an alias for Nullable<bool>
".
编辑:啊,我错过了一个事实,即“bool”后面的问号实际上是类型名称的一部分,而不是您在问问题的指示:)。那么,您的问题的答案是“是的,C#bool?
只是”的别名Nullable<bool>
。
回答by Curt Hagenlocher
Null primitives are just regular primitives wrapped in Nullable. Any appearances to the contrary are just the compiler and syntactical sugar.
Null 原语只是包裹在 Nullable 中的常规原语。任何相反的现象都只是编译器和语法糖。
回答by spoulson
A bool
is a value type, therefore it can't contain a NULL value. If you wrap any value type with Nullable<>
, it will give it that ability. Moreover, access methods to the value change by additional properties HasValue
and Value
.
Abool
是值类型,因此它不能包含 NULL 值。如果您用 包装任何值类型Nullable<>
,它将赋予它这种能力。此外,值的访问方法通过附加属性HasValue
和Value
.
But to the question: Nullable<bool>
and bool?
are aliases.
但问题是:Nullable<bool>
andbool?
是别名。
回答by samjudson
There is no difference between bool? b = null
and Nullable<bool> b = null
. The ?
is just C# compiler syntax sugar.
bool? b = null
和之间没有区别Nullable<bool> b = null
。这?
只是 C# 编译器语法糖。
回答by Mark Ingram
To access the value of the bool? you need to do the following:
访问布尔值?您需要执行以下操作:
bool? myValue = true;
bool hasValue = false;
if (myValue.HasValue && myValue.Value)
{
hasValue = true;
}
Note you can't just do:
请注意,您不能只做:
if (myValue)
{
hasValue = true;
}
回答by morechilli
No there is no difference. In summary:
没有没有区别。总之:
System.Boolean -> valid values : true, false
System.Boolean -> 有效值:真、假
bool -> alias for System.Boolean
bool -> System.Boolean 的别名
Nullable<bool> -> valid values : true, false, null
Nullable<bool> -> 有效值:真、假、空
bool? -> alias for Nullable<bool>
布尔?-> Nullable<bool> 的别名
Hope this helps.
希望这可以帮助。
回答by svick
I'm surprised nobody went to the source (the C# spec) yet. From §4.1.10 Nullable types:
我很惊讶还没有人去了解源代码(C# 规范)。从 §4.1.10 可空类型开始:
A nullable type is written T?, where T is the underlying type. This syntax is shorthand for System.Nullable<T>, and the two forms can be used interchangeably.
可空类型写为 T?,其中 T 是基础类型。此语法是 System.Nullable<T> 的简写,这两种形式可以互换使用。
So, no, there isn't any difference between the two forms. (Assuming you don't have any other type called Nullable<T>
in any of the namespaces you use.)
所以,不,这两种形式没有任何区别。(假设您没有Nullable<T>
在您使用的任何命名空间中调用任何其他类型。)
回答by AMC
No difference. Take a look here: http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
没有不同。看看这里:http: //msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
"The syntax T? is shorthand for Nullable, where T is a value type. The two forms are interchangeable."
“语法 T? 是 Nullable 的简写,其中 T 是一种值类型。这两种形式可以互换。”