Friday, October 21, 2005

Error ??

Check the following piece of code
--------------------------
ulong a=1234567;
const int b=1234;
Console.WriteLine(a-b);
--------------------
This Code compiles fine.No error at all.
However following version causes error.
--------------------------
ulong a=1234567;
int b=1234;
Console.WriteLine(a-b);
--------------------
Following error is shown by the compilerError :Operator '-' is ambiguous on operands of type 'ulong' and 'int'

The only differnce in both code is use of "const".

Lets analyze why this happens. There is no implicit conversion between ilong and int.Hence the compiler throws an error.However in the first case , since the variable is marked const, compiler knows its value at compile time and knows it wont change.It realizes the value is +ive and converts it to ulong.