Moorstream

Hi,

When I choose the return type for a method, I can choose between bool and Boolean. I am just curious: What is the difference between them and what is correct to use

Best

/M



Re: Visual C# General Which return type should I use: bool or Boolean?

timvw

They are the same.. (in c# bool is short for System.Boolean)...

I prefer to use the c# keywords.





Re: Visual C# General Which return type should I use: bool or Boolean?

Martin Xie - MSFT

In fact, bool and Boolean represent the same object in C#.

I think "bool" was provided as a syntactic sugar for folks switching from other languages like Java, C++, etc. The same goes for the following objects:


e.g.
long = Int64
int = Int32
short = Int16
string = String

The compiler replaces the native representation (long, int, short, etc.) with the proper CLR representation at compile time (with Int64, Int32, Int16, etc.).

In other words, bool is a alias for Boolean. If you want to use Boolean you must add "using System" statement, if you use bool you don't need to have "using System".

Glad to help you!

Best Regards,

Martin Xie






Re: Visual C# General Which return type should I use: bool or Boolean?

decyclone


Hi,

In addition to tim and martin's comments, I would like to recommend you use Boolean type instead of the bool keyword provided in C# that is just a syntax sugar as stated by martin. There are two reasons behind this, First, when you program in multiple .Net compatible languages (C#,VB,C++), it's easy to remember the types than each language's keywords. Second, when implementing common functionality for the .Net platform, like the Convert class. You'll see the Convert class uses ToBoolean instead of ToBool, because it's not developed for a single language but for entire .Net platform and all the supported languages can use it.

Hope this helps.





Re: Visual C# General Which return type should I use: bool or Boolean?

Matthew Watson

If you're going to go down that route, then you should also use System.Int32 instead of int, System.String instead of string, System.Single instead of float, and so on.

I personally do not like that; I prefer to use the keywords for C# (since I do not use Visual Basic).

The IDE's keyword highlighting will highlight "int", "short" etc in your selected keyword colour.
Also, when the IDE shows you the parameter types of a method, it uses the C# versions.

However, to each their own!