nullable types in c# code example
Example 1: set int to null c#
// Nullable types add null as a valid value in addition to the type range
Nullable<int> i = null;
// Can also be written as:
int? i = null;
Example 2: c# nullable generic
static void Main(string[] args)
{
int? i = GetValueOrNull<int>(null, string.Empty);
}
public static Nullable<T> GetValueOrNull<T>(DbDataRecord reader, string columnName) where T : struct
{
object columnValue = reader[columnName];
if (!(columnValue is DBNull))
return (T)columnValue;
return null;
}
Example 3: c# nullable
double? pi = 3.14;
char? letter = 'a';
int m2 = 10;
int? m = m2;
bool? flag = null;
// An array of a nullable value type:
int?[] arr = new int?[10];