Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant cast float to int if object

Tags:

c#

casting

This code runs fine

float ff = 5.5f;
int fd = (int) ff;

Console.Write(fd);

Where as this code doesnt

float ff = 5.5f;
object jf = ff;
int fd = (int) jf;

Console.Write(fd);

What rule in the runner causes this to happen?

like image 209
Dested Avatar asked Dec 04 '10 08:12

Dested


1 Answers

You can cast a float to an int, but you can't cast a boxed float to an int - you have to unbox it first.

int fd = (int)(float)jf;

Read Eric Lippert's post Representation and Identity for more details.

like image 118
Mark Byers Avatar answered Sep 17 '22 16:09

Mark Byers