Humza Tufail
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 ?
By Humza Tufail in .NET on Jan 07 2018
  • Humza Tufail
    Jan, 2018 7

    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)

    • 0