Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

c# - Why is this switch case returning a double even though it is an int?

Given following code:

object value = header.DataContentType switch
{
    DataContentType.DOUBLE => (double)BitConverter.ToDouble(currentValueBytes.ToArray()),
    DataContentType.FLOAT => (float)BitConverter.ToSingle(currentValueBytes.ToArray()),
    DataContentType.BYTE => (byte)contentData[i],
    DataContentType.SHORT => (short)BitConverter.ToInt16(currentValueBytes.ToArray()),
    DataContentType.INTEGER => (int)BitConverter.ToInt32(currentValueBytes.ToArray()),
    DataContentType.LONG => (long)BitConverter.ToInt64(currentValueBytes.ToArray()),
    _ => throw new InvalidDataException("Invalid data type"),
};

Now, in case when header.DataContentType is DataContentType.INTEGER, the value gets assigned as a double to value instead of as an int and (int)value causes an InvalidCastException

If I debug I can clearly see that it steps into the INTEGER case, and if I evaluate BitConverter.ToInt32(currentValueBytes.ToArray()) in the debug console I get an integer returned. However, as soon as the switch case is exited the variable value is of type double.

Further, if I manually do value = BitConverter.ToInt32(currentValueBytes.ToArray()) the variable is of the correct type int. Implying the switch statement has to change the type to double for some weird reason.

I would like the switch case to return whatever type the BitConverter returns. How can I make the BitConverter return the type of the correct BitConverter case?

question from:https://stackoverflow.com/questions/65927527/why-is-this-switch-case-returning-a-double-even-though-it-is-an-int

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm pretty sure things will work better if every case in a switch expression returns a consistent type. They casts you show are superfluous. Each of those BitConverter calls returns the type you expect.

But, remember, each of those things ends up being boxed as an object at some point in your code. It's probably better if you specify when and how that boxing takes place. Consider something like this instead:

object value = header.DataContentType switch
{
    DataContentType.DOUBLE => (object)BitConverter.ToDouble(currentValueBytes.ToArray()),
    DataContentType.FLOAT => (object)BitConverter.ToSingle(currentValueBytes.ToArray()),
    DataContentType.BYTE => (object)contentData[i],
    DataContentType.SHORT => (object)BitConverter.ToInt16(currentValueBytes.ToArray()),
    DataContentType.INTEGER => (object)BitConverter.ToInt32(currentValueBytes.ToArray()),
    DataContentType.LONG => (object)BitConverter.ToInt64(currentValueBytes.ToArray()),
    _ => throw new InvalidDataException("Invalid data type"),
};

I'm intrigued how you are going to consume value at this point. Unboxing is a delicate operation. The only time I've ever done something like this is upstream of a JSON-ification operation (where the Newtonsoft JSON package if very happy serializing boxed native value types).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...