1
Reply

public void MyFunction(double _values) {object objValue=_value;int convertedValue=(int)objValue;Console.WriteLine("Value is="+convertedValue.ToString()); }1) should this code compile Successfully ? 2) Any run Time Error and Exception ?

Humza Tufail

Humza Tufail

6y
126
0
Reply

    1) yes This code Compile Successfully 2) but at run time They will throw a InvalidCastOeration because _value is double which is "Value" type in c# when we converted it into object then must the Auto boxing happen, but on the other side when unboxing happen then the compatible type is the double so after unboxing we converted it into Int so the answer is int convertedValue=(int)(double)objValue; OR int convertedValue=Convert.ToInt32(objValue)