What C# data types can be nullable types?
Asked Answered
B

4

9

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

Blockus answered 14/5, 2010 at 12:59 Comment(1)
Nullable<byte[]> is not ok because arrays are reference types (even if the type they contain is a value type). Note that this means you can do byte[] b = null.Numbersnumbfish
H
20

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() { … }
Honorific answered 14/5, 2010 at 13:1 Comment(7)
@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. (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 ;-)Bailly
@TJCrowder: I, for one, would disagree …Honorific
Minor nitpick: All value types except the Nullable type itself. ie the follwoing is not valid: var i = new Nullable<Nullable<int>>();Carbonization
@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.Honorific
@Konrad: And I'm sure you have lots of company. :-) Didn't mean to troll, though. :-(Bailly
@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.Impartial
@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.Scree
G
2

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

Guyette answered 14/5, 2010 at 13:6 Comment(0)
S
1

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

Scree answered 14/5, 2010 at 13:3 Comment(1)
"It can be any value type including struct" - well, except for the 50% of available structs that are Nullable<T> ;pBrufsky
U
0

Any data type can be nullable. Nullable works for you because normal int is not nullable, so the conversion to nullable int is necessary.

Nullable<int[]> not works because int[] is already a nullable type.

Nullable does not support types that are already nullable.

Note, that you need to convert a type to be nullable only if he is not already a nullable type.

Value types are not nullable, value types are int, float, double, long etc, as well as structs you create.

While reference types, for example, are already nullable (classes for example).

You can use the ? operator to make the type to be a nullable type. Note, that even tho it's not a good practice if you would use the question mark operator on a type that is already nullable it will not raise an error.

int? myNullableVar = null;
int[]? MyNullableArr = null; //not necessary but does not raise an error
Ultranationalism answered 12/1, 2021 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.