使用常量文字初始化 ArrayList
下面的ArrayList是否可以直接初始化而不需要aFileExt字符串数组?
private static string[] aFileExt =
{"css", "gif", "htm", "html", "txt", "xml" };
private System.Collections.ArrayList alFileTypes =
new System.Collections.ArrayList(aFileExt);
下面的行是目标,但我的 .Net 编译器不喜欢它:
private static System.Collections.ArrayList alFileTypes =
new System.Collections.ArrayList({"css","gif","htm","html","txt","xml"});
我正在使用 .net 微框架,因此无法访问泛型类型。
Can the ArrayList below be initialized directly without the need for aFileExt string array?
private static string[] aFileExt =
{"css", "gif", "htm", "html", "txt", "xml" };
private System.Collections.ArrayList alFileTypes =
new System.Collections.ArrayList(aFileExt);
The line below is the goal, but my .Net Compiler does not like it:
private static System.Collections.ArrayList alFileTypes =
new System.Collections.ArrayList({"css","gif","htm","html","txt","xml"});
I am using the .net Micro Framework and thus do not have access to generic types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
C# 1 或 2:
C# 3 使用隐式类型数组:
C# 3 使用集合初始值设定项:
或者创建您自己的辅助方法:
那么:
顺便说一句,您不使用通用集合的任何原因?
C# 1 or 2:
C# 3 using an implicitly typed array:
C# 3 using a collection initializer:
Or create your own helper method:
then:
Any reason why you're not using the generic collections, btw?
如果您使用的是 .NET 2.0 或更高版本,则应该使用通用
List
类型(即使它是List
如果您使用的是 .NET 3.5 或更高版本,则可以使用以下语法:
但是,无论哪种方式,如果您想坚持使用 ArrayList,您都可以执行以下操作:
If you're using .NET 2.0 or greater, you should be using the generic
List<T>
type (even if it'sList<object>
, which would given you the same functionality asArrayList
).If you're using .NET 3.5 or greater, you can use this syntax:
Either way, however, if you want to stick with
ArrayList
, you can just do:使用通用
List
而不是ArrayList
的 C# 3.0:C# 3.0 with a generic
List<T>
, rather than anArrayList
:尝试
Try
是的,只需更改
为
yes, just change
to