C# 使用常量文字初始化 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1723112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:16:23  来源:igfitidea点击:

Initializing ArrayList with constant literal

c#.net.net-micro-framework

提问by MandoMando

Can the ArrayList below be initialized directly without the need for aFileExt string array?

下面的ArrayList可以直接初始化而不需要aFileExt字符串数组吗?

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:

下面的行是目标,但我的 .Net 编译器不喜欢它:

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.

我使用的是 .net Micro Framework,因此无法访问泛型类型。

采纳答案by Jon Skeet

C# 1 or 2:

C# 1 或 2:

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

C# 3 using an implicitly typed array:

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

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

C# 3 using a collection initializer:

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

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?

你有什么理由不使用泛型集合,顺便说一句?

回答by Sean Bright

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

回答by kemiller2002

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"});

回答by Adam Robinson

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).

如果您使用 .NET 2.0 或更高版本,您应该使用泛型List<T>类型(即使它是List<object>,它会给您与 相同的功能ArrayList)。

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

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

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:

但是,无论哪种方式,如果您想坚持使用ArrayList,您都可以这样做:

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

回答by TLiebe

Try

尝试

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

回答by Thomas Levesque

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

C# 3.0 使用泛型List<T>,而不是ArrayList

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