# Nullable类型

int myInt = 1;

int? myNullableInt = 1;

double? myDouble = 3.14159; double? myOtherDouble = null;

int? nFirst = null;
int  Second = 2;
nFirst = Second;
// 可以 nFirst = 123;
// 可以 Second = nFirst;
//也可以，因为此时 nFirst==123  nFirst = null;
// 可以 Second = nFirst;
// 抛出异常, Second 是一个非nullable型变量。

if (nFirst.HasValue) Second = nFirst;

int ValA = 10;
int? ValB = 3;
int? ValC = ValA * ValB;  //ValC==30
int ValA = 10;
int? ValB = null;
int? ValC = ValA * ValB;  //ValC==null
int ValA = 10;
int? ValB = null;
int? ValC = ValA + ValB;  //ValC仍然是null;

int ValA = 10;
int? ValB = 3;
int ValC = ValA * ValB;    // ValC 不是Nullable类型

int abc = 123;
int xyz = 890;

int? def = null;
int? uvw = 123;

Comparison Result

abc == xyz     // false
abc == def     // false
def == null    // true

abc == uvw     // true
uvw == null    // false
uvw != null    // true

Comparison Result
abc > uvw     // false, they are equal
abc < def     // false, def is null
uvw < def     // false, because def is null
def > null    // false, because right side is null
uvw > null    // false, because right side is null

returnValue = first ?? second;

int? ValA= 123; int? ValB = null;

int NewVarA = ValA ?? -1;
int NewVarB = ValB ?? -1;

• 写评论