初始化列表的区别
Difference in initialize the list
我已经像这样在 VS 2013 中初始化列表
public static readonly List<string> ImageExtensions = new List<string>() { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" };
它在 vs 2013 中运行良好,但在 vs 2005 中它会抛出错误,所以我在下面这样使用,
public static readonly List<string> ImageExtensions = new List<string>(new String[] { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" });
它在 vs 2013 和 vs 2005 中都工作正常..为什么第一个代码在 vs 2005 中不起作用以及有什么区别b/w 两个代码
不同之处在于第一种初始化方式是使用 collection initializers,它仅在 .NET 3.0 中引入,即在 Visual Studio 2008 中引入。因此它们不会在 [=17 中编译=] 2005.
第二种方法是使用 the constructor of List<T>
,自 .NET 2.0 中引入泛型以来就可用,因此在 Visual Studio 2005 中也可用。
我已经像这样在 VS 2013 中初始化列表
public static readonly List<string> ImageExtensions = new List<string>() { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" };
它在 vs 2013 中运行良好,但在 vs 2005 中它会抛出错误,所以我在下面这样使用,
public static readonly List<string> ImageExtensions = new List<string>(new String[] { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" });
它在 vs 2013 和 vs 2005 中都工作正常..为什么第一个代码在 vs 2005 中不起作用以及有什么区别b/w 两个代码
不同之处在于第一种初始化方式是使用 collection initializers,它仅在 .NET 3.0 中引入,即在 Visual Studio 2008 中引入。因此它们不会在 [=17 中编译=] 2005.
第二种方法是使用 the constructor of List<T>
,自 .NET 2.0 中引入泛型以来就可用,因此在 Visual Studio 2005 中也可用。