拆箱陷阱

Int32 i = 10;
            Object obj = new Object();
            try
            {
                obj = i;
                Console.Write("装箱成功\n");
               // Console.Write()
            }
            catch(Exception ex)
            {
                Console.Write("装箱失败");
                Console.Write(ex.ToString());
                return;

            }
            try
            {
                Int64 j = (Int64)obj;//这里会出现错误
                Console.Write("拆箱成功");
            }
            catch(Exception ex)
            {
                Console.Write("装箱失败");
                Console.Write(ex.ToString());
            }
            Console.Read();

 如要修改:Int64 j = (Int64)(Int32)obj

posted @   才高远志  阅读(115)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示