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

204,552

Solution 1

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

I don't believe there is one.

bool is just an alias for System.Boolean

Solution 3

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

Solution 4

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

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

Share:
204,552
Gary Willoughby
Author by

Gary Willoughby

Software engineer and published author with over twenty years experience of software design and development.

Updated on July 13, 2022

Comments

  • Gary Willoughby
    Gary Willoughby almost 2 years

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

  • aroon65
    aroon65 almost 16 years
    Out of interest - why would you use both? I advocate using one or the other. Either use the aliases or don't, otherwise the code looks messy and inconsistent.
  • Scott Dorman
    Scott Dorman almost 16 years
    I think it looks messy when you don't use both. Use the alias for declaring the datatype and use the actuall class name when accessing static methods: string x = String.Format("Today is: {0}", DateTime.Now);
  • aroon65
    aroon65 almost 16 years
    So you'd do: int i = Int32.Parse(...); ? I have a couple of problems with that. Firstly, VS will highlight differently by default (I know you can change this but most devs just use the default syntax highlighting). Secondly, searching is harder especially with longs (long / Int64).
  • AviD
    AviD almost 16 years
    Yes, that is the exact way it should be done. int is not the class name, you should not be calling methods on it. On the other hand, it is the builtin type, and defining Int32 i; is too verbose and not natural.
  • Beep beep
    Beep beep over 15 years
    So wouldn't bool be better for cross-platform compatibility?
  • Arne Claassen
    Arne Claassen almost 15 years
    mixing aliases and class names just adds nothing to code clarity. Pick one and stick with it, imho
  • Asim Sajjad
    Asim Sajjad over 14 years
    From the above link microsoft says The C# type keywords and their aliases are interchangeable But why we need Aliases, From my point of view Boolean is more meaningful then bool and Int32 is more meaningful then int then why aliases ???
  • aroon65
    aroon65 over 14 years
    @asim: laziness? It's less typing and avoids the need to import System. Personally, I prefer the aliases. Typing "int" is far quicker than typing "Int32".
  • Mike Chamberlain
    Mike Chamberlain over 13 years
    @asmin: It's a C thing. int, float etc are familiar keywords to C and C++ programmers, so Microsoft decided to use these aliases for consistency.
  • max
    max almost 13 years
    @Mikey I'm pretty sure that Java decided to use these aliases for consistency, and Microsoft decided to use Java for consistency... :-)
  • Rosdi Kasim
    Rosdi Kasim over 11 years
    @MaxWell In Java, boolean and Boolean is not the same thing. One is a primitive data type and the other is an object.
  • max
    max about 11 years
    yeah I know, I was just making a funny.
  • Michael12345
    Michael12345 over 10 years
    This alias just caused me a problem. I use bool because I thought it was the "correct" type, then I got confused why my "bool" MVC DisplayTemplate wasn't firing. Turns out I need to name it Boolean. If I realized Boolean was the official type and bool was just an alias for convenience, I'd have used Boolean from the outset.
  • Logan Pickup
    Logan Pickup over 8 years
    I was tripped up by this - it seems to be a bug in the documentation. I saw the VS2005 page first (it appears higher in Google rankings for me!), and thought it implied that bool could contain null, but Boolean couldn't. Even though there is a link from the older to the newer documentation, I didn't read the newer documentation thoroughly enough to notice the single ? difference.
  • Servy
    Servy almost 8 years
    bool and Boolean are not two different types, that one type is not a reference type, you can call a static method on that one type using either identifier, and you don't in fact need to call a ConvertTo method to convert it to another type.
  • Servy
    Servy almost 8 years
    Boolean is a value type, not a reference type.
  • Tanner - reinstate LGBT people
    Tanner - reinstate LGBT people over 7 years
    It's not correct that "bool and Boolean are both reference types". The words bool and Boolean both refer to the same type, and that type is a value type, not a reference type.
  • bvdb
    bvdb over 7 years
    Thanks, I was trying to check if C# acted the same as java in this field. You answer is the only one that compares it to java (even though maybe not intentionally). :)
  • Choi
    Choi about 5 years
    I agreed with Kent Boogaart's comment. Yes, int i = Int32.Parse is weird. But for java user, Using bool and boolean the alias when declaring Boolean class is not bad idea.
  • Nat
    Nat about 4 years
    It's System.Boolean rather than just Boolean. The using System; was showing up because it allowed Boolean to be properly interpreted as System.Boolean. Not really lighter so much as just less verbose.
  • Legends
    Legends about 4 years
    What is the difference between Bool and bool?
  • The incredible Jan
    The incredible Jan over 3 years
    @Timothy Macharia Who/what is wrong? What does "convert to null" mean?
  • bakalolo
    bakalolo over 3 years
    Thumbs down because this didn't answer the question on what is the difference.