运营商和QUOT;>"不能应用于输入“ULONG”和“INT” [英] Operator ">" cannot be applied to type 'ulong' and 'int'

查看:176
本文介绍了运营商和QUOT;>"不能应用于输入“ULONG”和“INT”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,想知道为什么C#编译器只给了我第二次​​的错误消息if语句。

 枚举权限:ULONG
{
    ViewListItems = 1L,
}

公共无效方法()
{
    INT面膜= 138612833;
    INT比较= 32;

    如果(掩模大于0及(ULONG)Permissions.ViewListItems> 32)
    {
        //作品
    }

    如果(掩模大于0及(ULONG)Permissions.ViewListItems>比较)
    {
        //运算符'>不能应用于类型为ULONG和INT的操作数
    }
}
 

解决方案

我一直在尝试与此,使用ILSpy检查输出,而这正是我所发现的。

显然,在第二种情况下,这是一个错误 - 你不能比较一个 ULONG INT ,因为没有一个类型,你既可以强制到。 A ULONG 可能过大了,和 INT 可能是负面的。

在你的第一个情况,但是,编译器是聪明的。它实现了常量 1 >常量 32 是不正确的,并且不包括你的如果语句在编译的输出在所有。 (应给予可达code警告。)这是相同的,如果你定义和使用 const int的,而不是文字,或者即使你投字面明确的(即(INT)32 )。

但后来不是的编译器的比较成功的一个 ULONG INT ,这是我们刚才说的是不可能的?

显然不是。那么什么的的事?

尝试,而不是做大意如下的东西。 (以输入和输出写入所以编译器不会编译任何东西。)

  const int的三十二= 32;
静态无效的主要(字串[] args)
{
    ULONG X = ulong.Parse(到Console.ReadLine());
    布尔GT = X>三十二;
    Console.WriteLine(GT);
}
 

这将编译,即使 ULONG 是一个变量,即使结果不知道在编译时。看看输出在ILSpy:

 私有静态无效的主要(字串[] args)
{
    ULONG X = ulong.Parse(到Console.ReadLine());
    布尔GT = X> 32uL; / *哦,看,一个ULONG。 * /
    Console.WriteLine(GT);
}
 

那么,编译器实际上是在治疗你的 const int的 ULONG 。如果您三十二= -1 中,code编译失败,即使我们后来才知道, GT 会的总是的是真实的。编译器本身无法比拟的一个 ULONG INT

另外请注意,如果你让 X A ,而不是 ULONG ,编译器生成 32L ,而不是 32 为一个整数,即使它没有有无的到。 (可以比较 INT 在运行时。)

这指向编译器不处理 32 ULONG 在第一种情况下,因为它的的到,仅仅因为它的可以的匹配 X 的类型。它不必强迫不断节约运行,而这仅仅是奖金,当胁迫应该由权利是不可能的。

I'm curious to know why the C# compiler only gives me an error message for the second if statement.

enum Permissions : ulong
{
    ViewListItems = 1L,
}

public void Method()
{
    int mask = 138612833;
    int compare = 32;

    if (mask > 0 & (ulong)Permissions.ViewListItems > 32)
    {
        //Works
    }

    if (mask > 0 & (ulong)Permissions.ViewListItems > compare)
    {
        //Operator '>' cannot be applied to operands of type 'ulong' and 'int'
    }
}

解决方案

I've been experimenting with this, using ILSpy to examine the output, and this is what I've discovered.

Obviously in your second case this is an error - you can't compare a ulong and an int because there isn't a type you can coerce both to. A ulong might be too big for a long, and an int might be negative.

In your first case, however, the compiler is being clever. It realises that const 1 > const 32 is never true, and doesn't include your if statement in the compiled output at all. (It should give a warning for unreachable code.) It's the same if you define and use a const int rather than a literal, or even if you cast the literal explicitly (i.e. (int)32).

But then isn't the compiler successfully comparing a ulong with an int, which we just said was impossible?

Apparently not. So what is going on?

Try instead to do something along the following lines. (Taking input and writing output so the compiler doesn't compile anything away.)

const int thirtytwo = 32;
static void Main(string[] args)
{
    ulong x = ulong.Parse(Console.ReadLine());
    bool gt = x > thirtytwo;
    Console.WriteLine(gt);
}

This will compile, even though the ulong is a variable, and even though the result isn't known at compile time. Take a look at the output in ILSpy:

private static void Main(string[] args)
{
    ulong x = ulong.Parse(Console.ReadLine());
    bool gt = x > 32uL;        /* Oh look, a ulong. */
    Console.WriteLine(gt);
}

So, the compiler is in fact treating your const int as a ulong. If you make thirtytwo = -1, the code fails to compile, even though we then know that gt will always be true. The compiler itself can't compare a ulong to an int.

Also note that if you make x a long instead of a ulong, the compiler generates 32L rather than 32 as an integer, even though it doesn't have to. (You can compare an int and a long at runtime.)

This points to the compiler not treating 32 as a ulong in the first case because it has to, merely because it can match the type of x. It's saving the runtime from having to coerce the constant, and this is just a bonus when the coercion should by rights not be possible.

这篇关于运营商和QUOT;>"不能应用于输入“ULONG”和“INT”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆