393

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

0

15 Answers 15

430

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

9
  • 5
    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 ??? Mar 18, 2010 at 11:39
  • 8
    @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". Mar 18, 2010 at 13:47
  • 9
    @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. Feb 3, 2011 at 1:34
  • 47
    @Mikey I'm pretty sure that Java decided to use these aliases for consistency, and Microsoft decided to use Java for consistency... :-)
    – max
    Aug 19, 2011 at 14:07
  • 20
    @MaxWell In Java, boolean and Boolean is not the same thing. One is a primitive data type and the other is an object. Mar 28, 2013 at 4:42
56

I don't believe there is one.

bool is just an alias for System.Boolean

27

They are one and the same.

bool is just an alias for Boolean.

20

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

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

18

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

2
  • 3
    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. Oct 27, 2015 at 20:16
  • @Timothy Macharia Who/what is wrong? What does "convert to null" mean? Dec 16, 2020 at 8:28
15

They are the same.

C# programmers tend to prefer bool. It's less typing and just feels more natural from someone coming from that language family. It also guarantees you get the actual System.Boolean type (where otherwise it's possible to make your own Boolean type in a different namespace and the type resolution could become ambiguous).

But if you're 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 and helps simplify conversion back and forth between C# and VB.Net.

11

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

1
  • 1
    So wouldn't bool be better for cross-platform compatibility?
    – Beep beep
    Mar 11, 2009 at 4:11
10

One is an alias for the other.

7

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.

6
  • 11
    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. Sep 25, 2008 at 17:42
  • 3
    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); Sep 25, 2008 at 17:49
  • 2
    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). Sep 25, 2008 at 18:20
  • 5
    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.
    – AviD
    Sep 25, 2008 at 19:14
  • 7
    mixing aliases and class names just adds nothing to code clarity. Pick one and stick with it, imho Sep 9, 2009 at 22:07
3

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

2

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;

2

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.

3
  • 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
    Aug 5, 2016 at 17:59
  • 4
    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. Jan 29, 2017 at 18:04
  • 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). :)
    – bvdb
    Feb 8, 2017 at 14:38
1

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.

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

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

1
  • Boolean is a value type, not a reference type.
    – Servy
    Aug 5, 2016 at 20:30
-2

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)

1
  • 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.
    – Nat
    May 14, 2020 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.