1

我们将 DateTime 的 MinValue 表示为 DateTime.MinValue 但它如何表示为 Byte[]?

当我给出以下内容时,

DALImage.TwinImage = Convert.IsDBNull(reader["TwinImage"]) ? 
    Byte[].MinValue : 
    (Byte[])reader["TwinImage"];
  1. 我收到错误,因为“字节”是一种类型,但像变量一样使用
  2. 预期语法错误值(在 Byte[] 的 '[]' 部分中)

请帮助作为 C# 编程的新手

4

1 回答 1

1

字节数组没有最小值——它作为一个概念没有意义。这就像问“购物清单的最小值是多少”。

我认为您要做的是获取一个的Byte 数组。

DALImage.TwinImage = Convert.IsDBNull(reader["TwinImage"]) ? 
    new Byte[0] : 
    (Byte[])reader["TwinImage"];

编辑:您的评论表明您真正想要的是一个包含 1 个元素的字节数组,其中该元素是一个字节的最小值。

那将是以下代码:

DALImage.TwinImage = Convert.IsDBNull(reader["TwinImage"]) ? 
    new Byte[1] { Byte.MinValue } : 
    (Byte[])reader["TwinImage"];

但是,这也可以使用default来编写,这在语义上可能更清晰。

DALImage.TwinImage = Convert.IsDBNull(reader["TwinImage"]) ? 
    new Byte[1] { default(Byte) } : 
    (Byte[])reader["TwinImage"];
于 2013-03-14T09:17:11.430 回答