C#中的bool和Boolean类型有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/134746/
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
What is the difference between bool and Boolean types in C#
提问by Gary Willoughby
What is the difference between bool
and Boolean
types in C#?
C# 中的bool
和Boolean
类型有什么区别?
采纳答案by Kent Boogaart
bool
is an alias for System.Boolean
just as int
is an alias for System.Int32
. See a full list of aliases here: Built-In Types Table (C# Reference).
bool
是 的别名,System.Boolean
就像int
的别名一样System.Int32
。在此处查看别名的完整列表:内置类型表(C# 参考)。
回答by bhinks
I don't believe there is one.
我不相信有一个。
bool
is just an alias for System.Boolean
bool
只是一个别名 System.Boolean
回答by MagicKat
They are one in the same. bool is just an alias for Boolean.
他们是一样的。bool 只是 Boolean 的别名。
回答by itsmatt
One is an alias for the other.
一个是另一个的别名。
回答by Joel Coehoorn
They are the same. Boolean helps simplify conversion back and forth between C# and VB.Net. Most C# programmers tend to prefer 'bool', but if you are in a shop where there's a lot of both VB.Net and C# then you may prefer Boolean because it works in both places.
他们是一样的。Boolean 有助于简化 C# 和 VB.Net 之间的来回转换。大多数 C# 程序员往往更喜欢“bool”,但如果您所在的商店有很多 VB.Net 和 C#,那么您可能更喜欢 Boolean,因为它在这两个地方都适用。
回答by Carra
As has been said, they are the same. There are two because bool is a C# keyword and Boolean a .Net class.
如前所述,它们是相同的。有两个,因为 bool 是 C# 关键字,而 Boolean 是 .Net 类。
回答by James Boother
bool is an alias for the Boolean class. I use the alias when declaring a variable and the class name when calling a method on the class.
bool 是 Boolean 类的别名。我在声明变量时使用别名,在类上调用方法时使用类名。
回答by Zach Burlingame
There is no difference - bool is simply an alias of System.Boolean.
没有区别 - bool 只是 System.Boolean 的别名。
http://msdn.microsoft.com/en-us/library/c8f5xwh7(VS.71).aspx
http://msdn.microsoft.com/en-us/library/c8f5xwh7(VS.71).aspx
回答by Ryan Buddicom
I realise this is many years later but I stumbled across this page from google with the same question.
我意识到这是很多年后的事了,但我从 google 偶然发现了这个页面,上面有同样的问题。
There is one minor difference on the MSDN page as of now.
到目前为止,MSDN 页面上有一个细微差别。
VS2005
VS2005
Note:
If you require a Boolean variable that can also have a value of null, use bool. For more information, see Nullable Types (C# Programming Guide).
笔记:
如果您需要一个也可以具有 null 值的布尔变量,请使用 bool。有关更多信息,请参阅可空类型(C# 编程指南)。
VS2010
VS2010
Note:
If you require a Boolean variable that can also have a value of null, use bool?. For more information, see Nullable Types (C# Programming Guide).
笔记:
如果您需要一个也可以具有 null 值的布尔变量,请使用 bool?。有关更多信息,请参阅可空类型(C# 编程指南)。
回答by B. Clay Shannon
Perhaps bool is a tad "lighter" than Boolean; Interestingly, changing this:
也许 bool 比 Boolean 更“轻”一点;有趣的是,改变这一点:
namespace DuckbillServerWebAPI.Models
{
public class Expense
{
. . .
public bool CanUseOnItems { get; set; }
}
}
...to this:
...到这个:
namespace DuckbillServerWebAPI.Models
{
public class Expense
{
. . .
public Boolean CanUseOnItems { get; set; }
}
}
...caused my cs file to sprout a "using System;" Changing the type back to "bool" caused the using clause's hair to turn grey.
...导致我的 cs 文件萌生了一个“使用系统”;将类型改回“bool”会导致 using 子句的头发变灰。
(Visual Studio 2010, WebAPI project)
(Visual Studio 2010,WebAPI 项目)