使用常量文字初始化 ArrayList

发布于 2024-08-11 04:13:30 字数 492 浏览 8 评论 0原文

下面的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

紫﹏色ふ单纯 2024-08-18 04:13:30

C# 1 或 2:

private static ArrayList alFileTypes = 
     new ArrayList(new string[] {"css","gif","htm","html","txt","xml"});

C# 3 使用隐式类型数组:

private static ArrayList alFileTypes = 
    new ArrayList(new[] {"css","gif","htm","html","txt","xml"});

C# 3 使用集合初始值设定项:

private static ArrayList alFileTypes = 
    new ArrayList{"css","gif","htm","html","txt","xml"};

或者创建您自己的辅助方法:

public static ArrayList CreateList(params object[] items)
{
    return new ArrayList(items);
}

那么:

static ArrayList alFileTypes = CreateList("css","gif","htm","html","txt","xml");

顺便说一句,您不使用通用集合的任何原因?

C# 1 or 2:

private static ArrayList alFileTypes = 
     new ArrayList(new string[] {"css","gif","htm","html","txt","xml"});

C# 3 using an implicitly typed array:

private static ArrayList alFileTypes = 
    new ArrayList(new[] {"css","gif","htm","html","txt","xml"});

C# 3 using a collection initializer:

private static ArrayList alFileTypes = 
    new ArrayList{"css","gif","htm","html","txt","xml"};

Or create your own helper method:

public static ArrayList CreateList(params object[] items)
{
    return new ArrayList(items);
}

then:

static ArrayList alFileTypes = CreateList("css","gif","htm","html","txt","xml");

Any reason why you're not using the generic collections, btw?

治碍 2024-08-18 04:13:30

如果您使用的是 .NET 2.0 或更高版本,则应该使用通用 List 类型(即使它是 List,这将为您提供与 ArrayList 具有相同的功能)。

如果您使用的是 .NET 3.5 或更高版本,则可以使用以下语法:

private static List<string> fileTypes = new List<string>()
{ 
    "css","gif","htm","html","txt","xml" 
};

但是,无论哪种方式,如果您想坚持使用 ArrayList,您都可以执行以下操作:

private static System.Collections.ArrayList alFileTypes = 
 new System.Collections.ArrayList(new object[] {"css","gif","htm","html","txt","xml"});

If you're using .NET 2.0 or greater, you should be using the generic List<T> type (even if it's List<object>, which would given you the same functionality as ArrayList).

If you're using .NET 3.5 or greater, you can use this syntax:

private static List<string> fileTypes = new List<string>()
{ 
    "css","gif","htm","html","txt","xml" 
};

Either way, however, if you want to stick with ArrayList, you can just do:

private static System.Collections.ArrayList alFileTypes = 
 new System.Collections.ArrayList(new object[] {"css","gif","htm","html","txt","xml"});
生活了然无味 2024-08-18 04:13:30

使用通用 List 而不是 ArrayList 的 C# 3.0:

private static List<string> alFileTypes =
    new List<string> {"css","gif","htm","html","txt","xml"};

C# 3.0 with a generic List<T>, rather than an ArrayList :

private static List<string> alFileTypes =
    new List<string> {"css","gif","htm","html","txt","xml"};
不再让梦枯萎 2024-08-18 04:13:30
private static System.Collections.ArrayList alFileTypes = 
 new System.Collections.ArrayList(new string [] {"css","gif","htm","html","txt","xml"});
private static System.Collections.ArrayList alFileTypes = 
 new System.Collections.ArrayList(new string [] {"css","gif","htm","html","txt","xml"});
吹泡泡o 2024-08-18 04:13:30

尝试

private static System.Collections.ArrayList alFileTypes =   new System.Collections.ArrayList(){"css","gif","htm","html","txt","xml"};

Try

private static System.Collections.ArrayList alFileTypes =   new System.Collections.ArrayList(){"css","gif","htm","html","txt","xml"};
嗳卜坏 2024-08-18 04:13:30

是的,只需更改

private static System.Collections.ArrayList alFileTypes = 
     new System.Collections.ArrayList({"css","gif","htm","html","txt","xml"});

private static System.Collections.ArrayList alFileTypes = 
     new System.Collections.ArrayList(new string[] {"css","gif","htm","html","txt","xml"});

yes, just change

private static System.Collections.ArrayList alFileTypes = 
     new System.Collections.ArrayList({"css","gif","htm","html","txt","xml"});

to

private static System.Collections.ArrayList alFileTypes = 
     new System.Collections.ArrayList(new string[] {"css","gif","htm","html","txt","xml"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文