C#中bool和Boolean类型有什么区别

时间:2021-08-06 16:12:26

What is the difference between bool and Boolean types in C#?

C#中bool和Boolean类型有什么区别?

14 个解决方案

#1


346  

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#参考)。

#2


41  

I don't believe there is one.

我不相信有一个。

bool is just an alias for System.Boolean

bool只是System.Boolean的别名

#3


22  

They are one in the same. bool is just an alias for Boolean.

他们是一样的。 bool只是布尔值的别名。

#4


18  

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

#5


14  

I realise this is many years later but I stumbled across this page from google with the same question.

我意识到这是多年以后但我偶然发现这个页面来自谷歌同样的问题。

There is one minor difference on the MSDN page as of now.

到目前为止,MSDN页面上存在一个细微差别。

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。有关更多信息,请参见Nullable Types(C#编程指南)。

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?。有关更多信息,请参见Nullable Types(C#编程指南)。

#6


13  

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#的商店,那么你可能更喜欢布尔,因为它在两个地方都有效。

#7


9  

One is an alias for the other.

一个是另一个的别名。

#8


9  

As has been said, they are the same. There are two because bool is a C# keyword and Boolean a .Net class.

如前所述,它们是一样的。有两个因为bool是C#关键字而布尔是.Net类。

#9


5  

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类的别名。我在类上调用方法时声明变量和类名时使用别名。

#10


2  

They are the same, Bool is just System.Boolean shortened. Use Boolean when you are with a VB.net programmer, since it works with both C# and Vb

它们是相同的,Bool只是缩短了System.Boolean。当你使用VB.net程序员时使用布尔值,因为它适用于C#和Vb

#11


1  

Note that Boolean will only work were you have using System; (which is usually, but not necessarily, included) (unless you write it out as System.Boolean). bool does not need using System;

请注意,布尔只有在您使用System时才有效; (通常,但不一定,包括)(除非您将其写为System.Boolean)。 bool不需要使用System;

#12


0  

bool is a primitive type, meaning that the value (true/false in this case) is stored directly in the variable. Boolean is an object. A variable of type Boolean stores a reference to a Boolean object. The only real difference is storage. An object will always take up more memory than a primitive type, but in reality, changing all your Boolean values to bool isn't going to have any noticeable impact on memory usage.

bool是一种原始类型,意味着值(本例中为true / false)直接存储在变量中。 Boolean是一个对象。 Boolean类型的变量存储对Boolean对象的引用。唯一真正的区别是存储。对象将总是占用比基本类型更多的内存,但实际上,将所有布尔值更改为bool不会对内存使用产生任何明显影响。

I was wrong; that's how it works in java with boolean and Boolean. In C#, bool and Boolean are both reference types. Both of them store their value directly in the variable, both of them cannot be null, and both of them require a "convertTO" method to store their values in another type (such as int). It only matters which one you use if you need to call a static function defined within the Boolean class.

我错了;这就是它在boolean和Boolean中的工作原理。在C#中,bool和Boolean都是引用类型。它们都将它们的值直接存储在变量中,它们都不能为null,并且它们都需要“convertTO”方法将它们的值存储在另一个类型(例如int)中。只有在需要调用Boolean类中定义的静态函数时,才会使用哪一个。

#13


-1  

Perhaps bool is a tad "lighter" than Boolean; Interestingly, changing this:

也许bool比布尔有点“轻”;有趣的是,改变这个:

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项目)

#14


-1  

bool is an alias for Boolean. What aliases do is replace one string of text with another (like search/replace-all in notepad++), just before the code is compiled. Using one over the other has no effect at run-time.

bool是Boolean的别名。别名的作用是在编译代码之前将一个文本字符串替换为另一个文本字符串(例如,在notepad ++中搜索/替换所有内容)。在运行时使用一个在另一个上没有影响。

In most other languages, one would be a primitive type and the other would be an object type (value type and reference type in C# jargon). C# does not give you the option of choosing between the two. When you want to call a static method defined in the Boolean class, it auto-magically treats Boolean as a reference type. If you create a new Boolean variable, it auto-magically treats it as a reference type (unless you use the Activator.CreateInstance method).

在大多数其他语言中,一个是原始类型,另一个是对象类型(C#术语中的值类型和引用类型)。 C#不允许您在两者之间进行选择。当您想要调用Boolean类中定义的静态方法时,它会自动将布尔值视为引用类型。如果您创建一个新的布尔变量,它会自动将其视为引用类型(除非您使用Activator.CreateInstance方法)。

#1


346  

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#参考)。

#2


41  

I don't believe there is one.

我不相信有一个。

bool is just an alias for System.Boolean

bool只是System.Boolean的别名

#3


22  

They are one in the same. bool is just an alias for Boolean.

他们是一样的。 bool只是布尔值的别名。

#4


18  

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

#5


14  

I realise this is many years later but I stumbled across this page from google with the same question.

我意识到这是多年以后但我偶然发现这个页面来自谷歌同样的问题。

There is one minor difference on the MSDN page as of now.

到目前为止,MSDN页面上存在一个细微差别。

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。有关更多信息,请参见Nullable Types(C#编程指南)。

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?。有关更多信息,请参见Nullable Types(C#编程指南)。

#6


13  

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#的商店,那么你可能更喜欢布尔,因为它在两个地方都有效。

#7


9  

One is an alias for the other.

一个是另一个的别名。

#8


9  

As has been said, they are the same. There are two because bool is a C# keyword and Boolean a .Net class.

如前所述,它们是一样的。有两个因为bool是C#关键字而布尔是.Net类。

#9


5  

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类的别名。我在类上调用方法时声明变量和类名时使用别名。

#10


2  

They are the same, Bool is just System.Boolean shortened. Use Boolean when you are with a VB.net programmer, since it works with both C# and Vb

它们是相同的,Bool只是缩短了System.Boolean。当你使用VB.net程序员时使用布尔值,因为它适用于C#和Vb

#11


1  

Note that Boolean will only work were you have using System; (which is usually, but not necessarily, included) (unless you write it out as System.Boolean). bool does not need using System;

请注意,布尔只有在您使用System时才有效; (通常,但不一定,包括)(除非您将其写为System.Boolean)。 bool不需要使用System;

#12


0  

bool is a primitive type, meaning that the value (true/false in this case) is stored directly in the variable. Boolean is an object. A variable of type Boolean stores a reference to a Boolean object. The only real difference is storage. An object will always take up more memory than a primitive type, but in reality, changing all your Boolean values to bool isn't going to have any noticeable impact on memory usage.

bool是一种原始类型,意味着值(本例中为true / false)直接存储在变量中。 Boolean是一个对象。 Boolean类型的变量存储对Boolean对象的引用。唯一真正的区别是存储。对象将总是占用比基本类型更多的内存,但实际上,将所有布尔值更改为bool不会对内存使用产生任何明显影响。

I was wrong; that's how it works in java with boolean and Boolean. In C#, bool and Boolean are both reference types. Both of them store their value directly in the variable, both of them cannot be null, and both of them require a "convertTO" method to store their values in another type (such as int). It only matters which one you use if you need to call a static function defined within the Boolean class.

我错了;这就是它在boolean和Boolean中的工作原理。在C#中,bool和Boolean都是引用类型。它们都将它们的值直接存储在变量中,它们都不能为null,并且它们都需要“convertTO”方法将它们的值存储在另一个类型(例如int)中。只有在需要调用Boolean类中定义的静态函数时,才会使用哪一个。

#13


-1  

Perhaps bool is a tad "lighter" than Boolean; Interestingly, changing this:

也许bool比布尔有点“轻”;有趣的是,改变这个:

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项目)

#14


-1  

bool is an alias for Boolean. What aliases do is replace one string of text with another (like search/replace-all in notepad++), just before the code is compiled. Using one over the other has no effect at run-time.

bool是Boolean的别名。别名的作用是在编译代码之前将一个文本字符串替换为另一个文本字符串(例如,在notepad ++中搜索/替换所有内容)。在运行时使用一个在另一个上没有影响。

In most other languages, one would be a primitive type and the other would be an object type (value type and reference type in C# jargon). C# does not give you the option of choosing between the two. When you want to call a static method defined in the Boolean class, it auto-magically treats Boolean as a reference type. If you create a new Boolean variable, it auto-magically treats it as a reference type (unless you use the Activator.CreateInstance method).

在大多数其他语言中,一个是原始类型,另一个是对象类型(C#术语中的值类型和引用类型)。 C#不允许您在两者之间进行选择。当您想要调用Boolean类中定义的静态方法时,它会自动将布尔值视为引用类型。如果您创建一个新的布尔变量,它会自动将其视为引用类型(除非您使用Activator.CreateInstance方法)。