装箱和拆箱

        static void Main()
        {
            int number;
            object thing;
            double bigNumber;

            number = 42;
            thing = number;

            //System.InvalidCastException:“指定的转换无效。”
            //bigNumber = (double) thing;

            bigNumber = (double) (int) thing;

            Console.ReadKey();
        }

如何避免呢???

1.合理的使用重载以及重写方法。

2.使用泛型

3.如果不可避免,显式的使用装箱。

原文地址:https://www.cnblogs.com/bhfdz/p/8868016.html