27

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'
    }
}
2
  • 1
    The reason for this error is clear. In the second example you are trying to perform an illegal operation with an integer. In the first example 32 is being treated as a ulong. You should actually make mask a ulong. Jun 18, 2012 at 16:03
  • 1
    @Ramhound Either that (make compare an ulong), or make compare a const variable, so const int compare = 32;. Constant expressions will happily convert automatically from int to ulong at compile-time if they are non-negative, something which goes under the name Implicit constant expression conversions. May 21, 2014 at 12:40

3 Answers 3

32

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.

4
  • 1
    This was a very interesting read, props to you for the research.
    – Amicable
    Jun 19, 2012 at 8:41
  • 1
    @Amicable - props to you for an interesting question that made me want to research it :)
    – Rawling
    Jun 19, 2012 at 8:49
  • 2
    The C# compiler is entirely in agreement with the C# Language Specification here. When an expression of type int is a compile-time constant, there do exist an implicit constant expression conversion from int to ulong provided that the value is within the range of ulong, i.e. non-negative, which is something the C# compiler can see with a constant expression. 32 is a constant expression of type int. When an expression of type int is not a compile-time constant, no implicit conversion exists to ulong. Example compare. May 21, 2014 at 12:36
  • 1
    "because there isn't a type you can coerce both to" -- thank you!
    – Brad
    Oct 19, 2018 at 2:04
21

It's not the CLR giving this error message it's the compiler.

In your first example the compiler treats 32 as ulong (or a type that's implicitly convertible to ulong eg uint) whereas in your second example you've explicitly declared the type as an int. There is no overload of the > operator that accepts an ulong and an int and hence you get a compiler error.

6
  • 7
    The integer literals page (msdn.microsoft.com/en-us/library/aa664674.aspx) says "If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong." Plus my Intellisense (tooltip) says the 32 is an int. But this does seem to be what's happening...
    – Rawling
    Jun 18, 2012 at 15:56
  • 1
    @Rawling Very true, I can see three possible reasons (I daresay there are many more): #1 perhaps because of differing interpretations of "value can be represented", ie the compiler knows that 32 cannot be an int since the code would not compile and tries uint next which succeeds. Intellisense obviously doesn't evaluate the entire expression to calculate the type, int is generally correct so why sacrifice computing time for an edge case. #2 the documentation for integer literals is outdated. Or #3 it's a compiler bug. Jun 18, 2012 at 16:06
  • @Rawling - There isn't a bug it is working exactly like it should. The literal 32 is being treated as a long. You even quote the statement where it says it will be treated as a long. Jun 18, 2012 at 16:15
  • @ramhound you can't compare a long either.
    – Rawling
    Jun 18, 2012 at 16:29
  • 1
    @rich Actually after further work with ILSpy, I can see that you're right. The compiler does actually treat 32, which by my reading should be treated as an int, as a ulong. Whether or not it goes on to compile it out is a separate issue.
    – Rawling
    Jun 19, 2012 at 7:18
3

rich.okelly and rawling's answers are correct as to why you cannot compare them directly. You can use the Convert class's ToUInt64 method to promote the int.

if (mask > 0 & (ulong)Permissions.ViewListItems > Convert.ToUInt64(compare))
{
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.