如何在c#中为null可空字节赋值null

Ali*_*zad 3 exception

advert.AC = String.IsNullOrEmpty(reader["AC"].ToString()) ? null : Byte.Parse(reader["AC"].ToString());
Run Code Online (Sandbox Code Playgroud)

我想在读取器["AC"]中存在空记录时将null分配给名为AC的属性,否则通过将其解析为Byte将值赋给AC.AC的类型是"Byte?" 在我的情况下,但它在上述作业中给出了错误.

无法确定条件表达式的类型,因为''和'byte'之间没有隐式转换C:\ Users\Waheed Ahmed\documents\visual studio 2010\Projects\Autos\Autos\Controllers\autosController.cs 274 85 Autos

小智 7

您可以在这里引用具有Nullable <value>类型的条件运算符赋值?

如果你确实需要将null转换为Byte?然后使用它,就像

advert.AC = String.IsNullOrEmpty(reader["AC"].ToString()) ? (Byte?)null : Byte.Parse(reader["AC"].ToString());
Run Code Online (Sandbox Code Playgroud)