What is the difference between bool and Boolean types in C#
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).
Gary Willoughby
Software engineer and published author with over twenty years experience of software design and development.
Updated on July 13, 2022Comments
-
Gary Willoughby almost 2 years
What is the difference between
bool
andBoolean
types in C#? -
aroon65 almost 16 yearsOut 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 almost 16 yearsI 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 almost 16 yearsSo 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 almost 16 yearsYes, 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 over 15 yearsSo wouldn't bool be better for cross-platform compatibility?
-
Arne Claassen almost 15 yearsmixing aliases and class names just adds nothing to code clarity. Pick one and stick with it, imho
-
Asim Sajjad over 14 yearsFrom 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 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 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 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 over 11 years@MaxWell In Java,
boolean
andBoolean
is not the same thing. One is a primitive data type and the other is an object. -
max about 11 yearsyeah I know, I was just making a funny.
-
Michael12345 over 10 yearsThis 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 over 8 yearsI 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, butBoolean
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 almost 8 years
bool
andBoolean
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 aConvertTo
method to convert it to another type. -
Servy almost 8 yearsBoolean is a value type, not a reference type.
-
Tanner - reinstate LGBT people over 7 yearsIt's not correct that "
bool
andBoolean
are both reference types". The wordsbool
andBoolean
both refer to the same type, and that type is a value type, not a reference type. -
bvdb over 7 yearsThanks, 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 about 5 yearsI 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 about 4 yearsIt's
System.Boolean
rather than justBoolean
. Theusing System;
was showing up because it allowedBoolean
to be properly interpreted asSystem.Boolean
. Not really lighter so much as just less verbose. -
Legends about 4 yearsWhat is the difference between
Bool
andbool
? -
The incredible Jan over 3 years@Timothy Macharia Who/what is wrong? What does "convert to null" mean?
-
bakalolo over 3 yearsThumbs down because this didn't answer the question on what is the difference.