简体   繁体   中英

What C# data types can be nullable types?

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

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

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

struct Nullable<T> where T : struct, new() { … }

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM