装箱和拆箱

        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.如果不可避免,显式的使用装箱。

posted on 2018-04-17 17:11  不会非的猪  阅读(120)  评论(0编辑  收藏  举报

导航