Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What C# data types can be nullable types?

Tags:

c#

nullable

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

like image 722
Randy Minder Avatar asked May 14 '10 12:05

Randy Minder


People also ask

What is C language?

What Does C Programming Language (C) Mean? C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.

What is C and why it is used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is History of C?

C is one of the high-level programming languages developed by Dennis Ritchie. C was originally developed for UNIX operating system to beat the issues of previous languages such as B, BCPL, etc. The UNIX operating system development started in the year 1969, and its code was rewritten in C in the year 1972.

What is C language in English?

The C programming language is a low-level (close to the computer) computer programming language that was developed to do system programming for the operating system UNIX and is an imperative programming language. C was developed in the early 1970s by Ken Thompson and Dennis Ritchie at Bell Labs.


2 Answers

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() { … }
like image 85
Konrad Rudolph Avatar answered Oct 08 '22 19:10

Konrad Rudolph


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

like image 43
Mike Avatar answered Oct 08 '22 19:10

Mike