Boolean, Int32

  • Thread starter Thread starter Peter Kirk
  • Start date Start date
P

Peter Kirk

Hi

is it it correct that variables of types like Boolean and Int32 cannot be
null?

It appears these types are structs, and not classes and therefore variables
cannot be assigned the value null (like DateTime as well I think). Is there
anyone else who finds this annoying?

Is the only way around this to wrap these types in your own class? (Why have
Boolean actually, when it really is only a bool primitive and not a class?)


Thanks
Peter
 
Peter said:
is it it correct that variables of types like Boolean and Int32 cannot be
null?
Yes.

It appears these types are structs, and not classes and therefore variables
cannot be assigned the value null (like DateTime as well I think).

Indeed - as documented on MSDN. (Look at the docs for any of these
types and you'll see that they're structs.)
Is there anyone else who finds this annoying?

Not very often, to be honest. Then again, I don't do much data binding
etc.
Is the only way around this to wrap these types in your own class?

Not quite. You could wrap them in your own structs which have an extra
field specifying whether they're "null" or not - that would make them
keep value semantics.

For 1.1, there's a NullableTypes project on SourceForge:
http://nullabletypes.sourceforge.net/

In C#/.NET 2.0, there's the concept of Nullable<T> built into the
language:
int? x = null;
(Why have Boolean actually, when it really is only a bool primitive and not a class?)

What do you mean here? There's just as much cause to have Boolean as a
type as there is to have Int32. Note that "bool" is just a C# shorthand
for "System.Boolean".

Jon
 
Jon Skeet said:
Indeed - as documented on MSDN. (Look at the docs for any of these
types and you'll see that they're structs.)


Not very often, to be honest. Then again, I don't do much data binding
etc.

Yes, it is exactly here I find difficulties - especially with Boolean for
which it is difficult to define a "null value" - there are only two valid
values as it is. With Int32 or DateTime one can of course define some value
as null (like the minimum or maximum allowable value for the respective
type).

Not quite. You could wrap them in your own structs which have an extra
field specifying whether they're "null" or not - that would make them
keep value semantics.

I'm not 100% following you here. Do you mean define a "MyInt32" struct which
contains an Int32, and a bool field for "IsNull" - which I then need to
check when I use MyInt32 (and ensure I set it correctly as well when I set
the value of the Int32)?

For 1.1, there's a NullableTypes project on SourceForge:
http://nullabletypes.sourceforge.net/

Thanks, I'll look at that.

In C#/.NET 2.0, there's the concept of Nullable<T> built into the
language:
int? x = null;

Yes, I did see this but I am not using .NET 2.

What do you mean here? There's just as much cause to have Boolean as a
type as there is to have Int32. Note that "bool" is just a C# shorthand
for "System.Boolean".

OK, I wasn't aware of that.

Thanks,
Peter
 
Peter Kirk said:
Yes, it is exactly here I find difficulties - especially with Boolean for
which it is difficult to define a "null value" - there are only two valid
values as it is. With Int32 or DateTime one can of course define some value
as null (like the minimum or maximum allowable value for the respective
type).
Indeed.


I'm not 100% following you here. Do you mean define a "MyInt32" struct which
contains an Int32, and a bool field for "IsNull" - which I then need to
check when I use MyInt32 (and ensure I set it correctly as well when I set
the value of the Int32)?

Yes - although you could use conversion operators (explicit or
implicit) to help there.
Thanks, I'll look at that.

Sounds like that's your best answer, given your other post.
Yes, I did see this but I am not using .NET 2.

Right. It wasn't clear from your post.
OK, I wasn't aware of that.

The same is true for all "built-in" types - all the "C#" types are
really just shorthands for CLR types.
 
Back
Top