Nullable Types - CSharp Language Basics

CSharp examples for Language Basics:Nullable

Introduction

Reference types can represent a nonexistent value with a null reference.

Value types, however, cannot ordinarily represent null values. For example:

string s = null;       // OK, Reference Type
int i = null;          // Compile Error, Value Type cannot be null

To represent null in a value type, you must use a special construct called a nullable type.

A nullable type is denoted with a value type followed by the ? symbol:

int? i = null;                     // OK, Nullable Type
Console.WriteLine (i == null);     // True

Related Tutorials