tags:

views:

299

answers:

4

Can someone give me a list, or point me to where I can find a list of C# data types that can be a nullable type?

For example:

I know that `Nullable<int>` is ok
I know that `Nullable<byte[]>` is not.

I'd like to know which types are nullable and which are not. BTW, I know I can test for this at runtime. However, this is for a code generator we're writing, so I don't have an actual type. I just know that a column is "string" or "int32" etc.

Thanks.

+17  A: 

All value types (except Nullable<T> itself) can be used in nullable types – i.e. all types that derive from System.ValueType (that also includes enums!).

The reason for this is that Nullable is declared something like this:

struct Nullable<T> where T : struct, new() { … }
Konrad Rudolph
@OP: ...because the whole point is to fix the issue (I would call it a major design flaw; others would disagree) with value types not being nullable. (http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx) And they sure didn't help matters any by calling it `Nullable`! Because of things exactly like your question "What types are nullable?" Answer: Reference types are nullable. Value types are compatible with `Nullable`, which is the opposite. *sigh* ;-)
T.J. Crowder
@TJCrowder: I, for one, would disagree …
Konrad Rudolph
Minor nitpick: All value types *except* the Nullable type itself. ie the follwoing is not valid: var i = new Nullable<Nullable<int>>();
Rob Levine
@Randy Minder: There can be no complete list of types since C# has an extensible type system. Everyone can write their own value types. Using such a list would inevitably result in buggy, hard to maintain code. Furthermore, you can test for that at runtime **even in your code generator**, provided that the code generator is written in .NET.
Konrad Rudolph
@Konrad: And I'm sure you have lots of company. :-) Didn't mean to troll, though. :-(
T.J. Crowder
@Randy Minder: There is no complete list; users are free to define new value types. You can test if a type is a value type by checking `System.Type.IsValueType`.
Michael Petito
@Randy, complete list? You can certainly check MSDN for the BCL value types, but even that will not account for every single value type you'll come across in code, be it your own or a third party library.
Anthony Pegram
@Rob Levine: **valuable** nitpick. Thanks.
Konrad Rudolph
+1  A: 

It can be any value type including struct, it cannot be a reference type, as those are inherently nullable already.

Yes: Int32 double DateTime CustomStruct etc.

No: string Array CustomClass etc.

For more information, see MSDN: http://msdn.microsoft.com/en-us/library/2cf62fcy(v=VS.80).aspx

Anthony Pegram
+4  A: 

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

JoelFan
This answer got accepted? Wow.
Konrad Rudolph
Yeah, he asked for a list, not an explanation :)
JoelFan
+1  A: 

A type is said to be nullable if it can be assigned a value or can be assigned null, which means the type has no value whatsoever. Consequently, a nullable type can express a value, or that no value exists. For example, a reference type such as String is nullable, whereas a value type such as Int32 is not. A value type cannot be nullable because it has enough capacity to express only the values appropriate for that type; it does not have the additional capacity required to express a value of null.

The Nullable structure supports using only a value type as a nullable type because reference types are nullable by design.

The Nullable class provides complementary support for the Nullable structure. The Nullable class supports obtaining the underlying type of a nullable type, and comparison and equality operations on pairs of nullable types whose underlying value type does not support generic comparison and equality operations.

From Help Docs http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

Mike