Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
int x = 2.5;
Console.WriteLine(x);It does not compile
- Expecting
2: C# never drops a fraction silently, so putting2.5into anintstops the build with errorCS0266. - Expecting an error only at run time: the compiler already sees a
doublegoing into anintvariable, so no program is built and nothing runs.
- How would you store
2.5in aninton purpose, dropping the fraction? - Why does
double y = 2;compile whenint x = 2.5;does not?