When I want to cast 1 to double, it does not work. When I cast 1.0 to double it works. Why?
I have the following code:
static void Main(string[] args)
{
ArrayList liste1 = new ArrayList();
liste1.Add(1);
liste1.Add("Hallo");
liste1.Add(2.5);
double num = (double)liste1[0] + (double)liste1[2];
Console.WriteLine(num);
Console.ReadLine();
}
When I change liste1.Add(1); to liste1.Add(1.0); it works. Why does it work with 1.0 and not with 1?
It works too when I first cast liste1[0] to an int, and then to a double. Can you tell me why, please?
Thank you
ArrayListholds objects. If you add anintit gets “boxed” into an object and can only be “unboxed” back toint– notdouble.MSDN: boxing & unboxing