Differencebetween Bool and Boolean Types in C#

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

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

whats different in bool and boolean in c# .net

As MSDN states:
"The bool keyword is an alias of System.Boolean"

No difference.

Difference between Boolean and bool in c#?

They are the same. If you mouse over the lowercase one it shows that it is an alias to the uppercase version.

Note that by convention you should always use the lowercase version

What's the difference between bool and bool??

The ? symbol after a type is only a shortcut to the Nullable type, bool? is equivalent to Nullable<bool>.

bool is a value type, this means that it cannot be null, so the Nullable type basically allows you to wrap value types, and being able to assign null to them.

bool? can contain three different values: true, false and null.

Also, there are no short-circuiting operators (&& ||) defined for bool?

Only the logical AND, inclusive OR, operators are defined and they behave like this:

x        y      x & y   x | y   
true true true true
true false false true
true null null true
false true false true
false false false false
false null false null
null true null true
null false false null
null null null null

The Nullable type is basically a generic struct, that has the following public properties:

public struct Nullable<T> where T: struct
{
public bool HasValue { get; }
public T Value { get; }
}

The HasValue property indicates whether the current object has a value, and the Value property will get the current value of the object, or if HasValue is false, it will throw an InvalidOperationException.

Now you must be wondering something, Nullable is a struct, a value type that cannot be null, so why the following statement is valid?

int? a = null;

That example will compile into this:

.locals init (valuetype [mscorlib]System.Nullable`1<int32> V_0)
IL_0000: ldloca.s V_0
IL_0002: initobj valuetype [mscorlib]System.Nullable`1<int32>

A call to initobj, which initializes each field of the value type at a specified address to a null reference or a 0 of the appropriate primitive type.

That's it, what's happening here is the default struct initialization.

int? a = null;

Is equivalent to:

Nullable<int> a = new Nullable<int>();

C# string vs String, bool vs Boolean

They are the same things - string is just an alias for System.String and bool for System.Boolean

What is the difference between BOOL and bool?

bool is a built-in C++ type while BOOL is a Microsoft specific type that is defined as an int. You can find it in windef.h:

typedef int                 BOOL;

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif

The values for a bool are true and false, whereas for BOOL you can use any int value, though TRUE and FALSE macros are defined in the windef.h header.

This means that the sizeof operator will yield 1 for bool (the standard states, though, that the size of bool is implementation defined), and 4 for BOOL.

Source: Codeguru article

Correctly distinguish between bool? and bool in C#

Code for getting class instance values:

// create class instance
InstViewModel model = new InstViewModel()
{
Uk = true,
UkNrs = false,
};

// check all boolean fields are false or null
bool isAllNullOrFalse = (from property in typeof(InstViewModel).GetProperties()
let type = property.PropertyType
let isBool = type == typeof(bool)
where isBool || type == typeof(bool?)
let value = property.GetValue(model)
select value == null || (isBool && bool.Equals(value, false))).All(e => e);

Console.WriteLine("All values are null or false = {0}", isAllNullOrFalse);

.Net, C# - Boolean.Equal differs from == compare

Sorry for this late edit, but I have found what has gone wrong. We read the property out of our database and our 'boolean' is represented by -1 not 1.
Automapper uses dynamically generated IL code to read from a datareader and because they do this, automapper is able to write -1 to the bool field. This results in a value of 0xff in out RAM.
If we now look at the il code for the equals method then we understand wy .equal is not ==.

.method public hidebysig newslot virtual final 
instance bool Equals(bool obj) cil managed
{
.custom instance void __DynamicallyInvokableAttribute::.ctor() = ( 01 00 00 00 )
// Code size 6 (0x6)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldind.u1
IL_0002: ldarg.1
IL_0003: ceq
IL_0005: ret
} // end of method Boolean::Equals

This loads an 1 and our value to the stack and compares both. Due to the fact that in our RAM the bool is -1 (0xFF) the comparison will return false.

See also this item on github.com for more information.

Difference between bool/Boolean, string/String etc

Short answer, there is no difference. They are just alias of each other, see

http://msdn.microsoft.com/en-us/library/ya5y69ds(VS.80).aspx for a complete list.



Related Topics



Leave a reply



Submit