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

    Question

  • Hi everybody,

    Could you tell me the difference between two keyworks Boolean and bool in C#?

    Thanks in advance

    pvphuc.


    Friday, July 27, 2007 4:27 AM

Answers

All replies

  • bool is just an alias for boolean.

     

    Friday, July 27, 2007 5:21 AM
  • Thanks for your help.

    Regards,

    pvphuc
    Friday, July 27, 2007 7:24 AM
  • Hi, So that can we use one instead of the other at the same time? I mean can we use bool in one place and boolean in another place? Why bool comes in dark blue and boolean comes in light blue as if it is something user-defined? Can someone help? Thanks in advance Regards, Jayani
    Thursday, April 02, 2009 12:20 PM
  • Yes, you can use bool or Boolean... it's the same. Although consistency is important, and you should use bool.
    The reason bool comes in dark blue is because it is a C# keyword, and Boolean in light blue is because it's a type (framework types and user-defined types are shown in the same color).

    Regards,
    Fernando.
    /* No comments */
    Thursday, April 02, 2009 12:24 PM
  • Hi, So that can we use one instead of the other at the same time? I mean can we use bool in one place and boolean in another place? Why bool comes in dark blue and boolean comes in light blue as if it is something user-defined? Can someone help? Thanks in advance Regards, Jayani

    The reason why "bool" is in dark blue and "Boolean" is in light blue is that "Boolean" is the actual type name, while "bool" is the keyword in C# that is an alias for this type.  All C# keywords are in dark blue, and all class names are in light blue. 

    You can use both interchangeably if you'd like.  It won't cause any performance implications, and the compiled code will look exactly the same, but it would be better practice to pick one or the other and use it consistently across your entire application.  I've heard good reasons to use "bool" and good reasons to use "Boolean", but regardless of whose reasons you subscribe to, be consistent.
    David Morton - http://blog.davemorton.net/
    Thursday, April 02, 2009 1:46 PM
    Moderator
  • I wonder if this thread will hit 100 posts like the Int vs Int32 thread...  ;P

    Thursday, April 02, 2009 3:02 PM
  • Maybe it will hit 1000 views, too.  There is a difference when it comes which Framework that you are targeting.  But, the difference is under the hood and should not affect code compilation or cause run time exceptions.

    Iwould seem that the Compact Framework does a switcheroo on the type Boolean and the keyword bool. 
    The Compact FCL uses 8 bits for a boolean, while the full-blown FCL uses 32 bits. 

    QUOTE

    While this behavior is the same on the full .NET Framework, the .NET Compact Framework also includes System.Char (Char in VB, char in C#), System.String (String in VB, string in C#), and System.Boolean (Boolean in VB, bool in C#) as blittable types. In the case of System.Char and System.String, this is due to the fact that, as mentioned previously, the .NET Compact Framework only supports Unicode, and so the marshaler always marshals the former as a 2-byte Unicode char and the latter as a Unicode array. In the case of System.Boolean, the .NET Compact Framework marshaler uses a 1-byte integer value, whereas the full .NET framework uses a 4-byte integer value.

    /QUOTE

    http://msdn.microsoft.com/en-us/library/aa446536.aspx


    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, April 02, 2009 4:13 PM
    Moderator
  • Be thankful you're not working in C++ where there are literally hundreds of common aliases (defined in typedef's) for every data type.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Thursday, April 02, 2009 4:18 PM