Wish that C# value types (primitives) could be assigned a null value!

  • Thread starter Thread starter Earl Teigrob
  • Start date Start date
E

Earl Teigrob

One of the few things about C# that drives me crazy is that I can not assign
a null value to a primitive data type such as "int" or "DateTime". There are
just so many times when I want to specify one of these types with "no valid
data" but it always must contain some valid value. In some cases I box and
unbox them as object types so that I can assign then a null value, but then
I lose the strong type casting of C#. From a technical point of view I
understand why this is the case and realize that it would probably take
unacceptable overhead for the language to be written so that primitive types
can accept a null values, but it would sure make my programming life easier!

I am thinking of using something like int.MinValue and DateTime.MinValue as
my own "null" value and test for these when I want to know if valid data
exist. If I do not know that data type before testing for my new null
conditions, I thought I could create my own NullData class and use
overloaded operators == and != that would test for my own set of null
conditions, based on the type that was being tested.

Does anyone have a way that they deal with this issue or seen any articles
that address this? I'm know I'm not the first one to tackle this!!!

Thanks for you feedback

Earl
 
Earl,

For now, I would use the INullable interface. With the advent of
generics, I can't imagine that there won't be a generic version of this
interface (or a class implementation) which will address this issue (where
you expose the value, and whether or not it is null).

Hope this helps.
 
Earl,
Have you considered the "primitive data type" like structures in
System.Data.SqlTypes namespace? (such as SqlInt32, SqlBoolean?)

They implement the INullable interface, giving them 'null-ness', at least in
the Database sense of the word, not necessarily in the OO sense of the word.

I understand that Whidbey will have a Generic type that will allow
"primitive data types" to appear to have "null-ness" also.

I don't have a link, I understand that there is an class library on the net
someplace that is similar to the System.Data.SqlTypes namespace.

Hope this helps
Jay
 
Nicholas,
I found the link shortly after posting my other message:

The Generic structure for "null-ness" in Whidbey:

http://longhorn.msdn.microsoft.com/lhsdk/ref/ns/system/s/optionalvalue/optionalvalue.aspx

Be warned I've seen two different names for the above structure.

Hope this helps
Jay

Nicholas Paldino said:
Earl,

For now, I would use the INullable interface. With the advent of
generics, I can't imagine that there won't be a generic version of this
interface (or a class implementation) which will address this issue (where
you expose the value, and whether or not it is null).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Earl Teigrob said:
One of the few things about C# that drives me crazy is that I can not assign
a null value to a primitive data type such as "int" or "DateTime". There are
just so many times when I want to specify one of these types with "no valid
data" but it always must contain some valid value. In some cases I box and
unbox them as object types so that I can assign then a null value, but then
I lose the strong type casting of C#. From a technical point of view I
understand why this is the case and realize that it would probably take
unacceptable overhead for the language to be written so that primitive types
can accept a null values, but it would sure make my programming life easier!

I am thinking of using something like int.MinValue and DateTime.MinValue as
my own "null" value and test for these when I want to know if valid data
exist. If I do not know that data type before testing for my new null
conditions, I thought I could create my own NullData class and use
overloaded operators == and != that would test for my own set of null
conditions, based on the type that was being tested.

Does anyone have a way that they deal with this issue or seen any articles
that address this? I'm know I'm not the first one to tackle this!!!

Thanks for you feedback

Earl
 
Back
Top