简体   繁体   English

为什么C#是关键字,对于double返回true,但是对于float而言为false,即使转换为float也有效?

[英]Why does C# is keyword return true for double, but false for float even though casting to float works?

Motivation: I have a method that returns a dynamic datatype. 动机:我有一个返回动态数据类型的方法。 The value is coming from a database and I know that the value will either be a float, double, or string. 该值来自数据库,我知道该值将是float,double或string。 I don't want to use the value if it is a string, so I wrote the following code: 如果它是一个字符串,我不想使用该值,所以我写了以下代码:

if (value is float)
{
    myVariable = (float)value;
}

My expectation was that this code would execute whether the actual type of value was double or float because of the following snippet from the documentation of the 'is' keyword: 我的期望是,由于'is'关键字的文档中的以下片段,此代码将执行,无论实际类型的值是double还是float:

An is expression evaluates to true if the provided expression is non-null,and the provided object can be cast to the provided type without causing an exception to be thrown. 如果提供的表达式为非null,则is表达式求值为true,并且可以将提供的对象强制转换为提供的类型,而不会引发异常。

Found here: is (C# Reference) 在这里找到: 是(C#参考)

However, when the type is double, (value is float) returns false and the assignment does not execute. 但是,当类型为double时,(value为float)返回false并且赋值不会执行。 But, I changed my code to this: 但是,我将我的代码更改为:

if (value is double)
{
    myVariable = (float)value;
}

and it works fine when the type of value is double - even though according to the documentation I shouldn't be able to cast to a float because (value is float) returns false. 当值的类型为double时,它工作正常 - 即使根据文档我不能转换为float,因为(value is float)返回false。

My Question: Why does (value is float) return false in the case where value is a double (which can be cast to a float without exception)? 我的问题:为什么(value是float)在value为double的情况下返回false(可以无异常地转换为float)?

EDIT - Short program demonstrating 编辑 - 短节目演示

class Program
{
    static void Main(string[] args)
    {
        dynamic d = 1.0;
        Console.WriteLine(d is double);
        Console.WriteLine(d is float);
        float f = (float)d;
        Console.WriteLine(f);
        Console.ReadKey();
    }
}

Right, this is because the type is dynamic . 对,这是因为类型是dynamic That basically means the meaning of the float cast depends on the execution-time type of the value. 这基本上意味着float的含义取决于值的执行时类型。

The is operator is checking whether float and double are the same type - and they're not, which is why it's returning false. is运算符正在检查floatdouble是否是同一类型 - 而它们不是,这就是为什么它返回false。

However, there is an explicit conversion from double to float , which is why the cast is working. 但是从显式转换doublefloat ,这就是为什么中投工作。 I wouldn't use that MSDN C# reference to work out the finer details of how the language behaves - the language specification is normally a better bet. 我不会使用MSDN C#引用来计算语言行为的更精细细节 - 语言规范通常是更好的选择。

Not sure, but I guess it is because there is no implicit conversion. 不确定,但我想这是因为没有隐式转换。 See Implicit Numeric Conversions Table (C# Reference) 请参阅隐式数字转换表(C#参考)

You explicitly casting double value to float variable. 您显式地将double值转换为float变量。 It is perfectly fine. 这很好。 When you checking the type, it is exact type match. 检查类型时,它是精确类型匹配。 What you need is: 你需要的是:

if (value is double || value is float)
{
    myVariable = (float)value;
}

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

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