C# C# Basics (Retired) Perfect Integers and Doubles

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Why C# can compile "int x = 8 / 3", but refuses to compile "int x = 2.9"

Why C# can compile "int x = 8 / 3", but refuses to compile "int x = 2.9"

2 Answers

Stuart Wright
Stuart Wright
41,069 Points

Because in C#, if you divide an integer by an integer, you will get an integer back. 9 / 3 would be 3 as per normal division. 8 / 3 results in 2. It rounds down to the nearest integer.

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Thanks for clear and fast answer Stuart. What a cool name