简体   繁体   English

为什么即使它是 int 这个 switch case 返回一个 double ?

[英]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现在,如果header.DataContentTypeDataContentType.INTEGER ,则该值将作为双精度value而不是作为int分配,并且(int)value会导致 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.如果我调试,我可以清楚地看到它进入了INTEGER案例,如果我在调试控制台中评估BitConverter.ToInt32(currentValueBytes.ToArray()) ,我会得到一个 integer 返回。 However, as soon as the switch case is exited the variable value is of type double.但是,一旦退出 switch case,变量value的类型就是 double。

Further, if I manually do value = BitConverter.ToInt32(currentValueBytes.ToArray()) the variable is of the correct type int .此外,如果我手动执行value = BitConverter.ToInt32(currentValueBytes.ToArray())变量的类型是正确的int Implying the switch statement has to change the type to double for some weird reason.暗示 switch 语句必须将类型更改为 double 出于某种奇怪的原因。

I would like the switch case to return whatever type the BitConverter returns.我希望开关盒返回BitConverter返回的任何类型。 How can I make the BitConverter return the type of the correct BitConverter case?如何使BitConverter返回正确的 BitConverter 案例的类型?

I'm pretty sure things will work better if every case in a switch expression returns a consistent type.如果switch表达式中的每个 case 返回一致的类型,我很确定事情会更好。 They casts you show are superfluous.你展示的演员阵容是多余的。 Each of those BitConverter calls returns the type you expect.这些 BitConverter 调用中的每一个都返回您期望的类型。

But, remember, each of those things ends up being boxed as an object at some point in your code.但是,请记住,这些东西中的每一个最终都会在您的代码中的某个位置被装箱为object 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.我很好奇你现在将如何消费value 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).我唯一一次做过这样的事情是在 JSON 化操作的上游(如果很高兴序列化盒装本机值类型,Newtonsoft JSON package)。

Replace the switch expression with an actual switch statement assigning value .switch表达式替换为分配value的实际switch语句。 A switch expression has a single return type that is the "best" type from all the types of the cases, and you will get a compiler error when there is no "best" type. switch表达式有一个返回类型,它是所有案例类型中的“最佳”类型,当没有“最佳”类型时,您将收到编译器错误。 So every case is converted to that "best" type.因此,每个案例都转换为“最佳”类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM