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

C#TypesBoolean

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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).

Solution 2 - C#

I don't believe there is one.

bool is just an alias for System.Boolean

Solution 3 - C#

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

Solution 4 - C#

There is no difference - bool is simply an alias of System.Boolean.

http://msdn.microsoft.com/en-us/library/c8f5xwh7(VS.71).aspx

Solution 5 - C#

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.

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).

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).

Solution 6 - C#

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.

Solution 7 - C#

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

Solution 8 - C#

One is an alias for the other.

Solution 9 - C#

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.

Solution 10 - C#

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

Solution 11 - C#

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.

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.

Solution 12 - C#

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;

Solution 13 - C#

No actual difference unless you get the type string. There when you use reflection or GetType() you get {Name = "Boolean" FullName = "System.Boolean"} for both.

Solution 14 - C#

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.

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).

Solution 15 - C#

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

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.

(Visual Studio 2010, WebAPI project)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGary WilloughbyView Question on Stackoverflow
Solution 1 - C#Kent BoogaartView Answer on Stackoverflow
Solution 2 - C#bhinksView Answer on Stackoverflow
Solution 3 - C#MagicKatView Answer on Stackoverflow
Solution 4 - C#Zach BurlingameView Answer on Stackoverflow
Solution 5 - C#Ryan BuddicomView Answer on Stackoverflow
Solution 6 - C#Joel CoehoornView Answer on Stackoverflow
Solution 7 - C#CarraView Answer on Stackoverflow
Solution 8 - C#itsmattView Answer on Stackoverflow
Solution 9 - C#James BootherView Answer on Stackoverflow
Solution 10 - C#8176135View Answer on Stackoverflow
Solution 11 - C#NateView Answer on Stackoverflow
Solution 12 - C#James CurranView Answer on Stackoverflow
Solution 13 - C#Stefanos ZilellisView Answer on Stackoverflow
Solution 14 - C#Michael KaldwidView Answer on Stackoverflow
Solution 15 - C#B. Clay Shannon-B. Crow RavenView Answer on Stackoverflow