سؤال

We represent MinValue of DateTime as DateTime.MinValue but how is it represented for Byte[]?

When I gave the below,

DALImage.TwinImage = Convert.IsDBNull(reader["TwinImage"]) ? 
    Byte[].MinValue : 
    (Byte[])reader["TwinImage"];
  1. I get the error as 'byte' is a type but use like a variable
  2. syntax error value expected (in '[]' part of Byte[])

Please Help as am a newbie to C# programming

هل كانت مفيدة؟

المحلول

An array of bytes doesn't have a minimum value - it doesn't make sense as a concept. It's like asking "What's the minimum value of a shopping list".

I think what you are trying to do is get an empty Byte array.

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

EDIT: Your comment suggests that what you actually want is a byte array with 1 element in it, where that element is the minimum value of a byte.

That would be the following code:

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

However, this could also be written using default, which is probably semantically cleaner.

DALImage.TwinImage = Convert.IsDBNull(reader["TwinImage"]) ? 
    new Byte[1] { default(Byte) } : 
    (Byte[])reader["TwinImage"];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top