foreach中的隐式类型转换

            List<object> list = new List<object> { "This is a string" };

            foreach (string entry in list)
            {
                Console.WriteLine(entry);
            }

在上面的foreach循环中实际上会发生隐式的类型转换,将list中的每一项从object强制转换为string,如果list中添加了非string类型的对象,在运行时就会抛出转换异常。

原文地址:https://www.cnblogs.com/lbsong/p/1671259.html