Initializing ArrayList with constant literal
Asked Answered
S

6

48

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.

Salzburg answered 12/11, 2009 at 15:36 Comment(0)
T
73

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?

Torrential answered 12/11, 2009 at 15:38 Comment(6)
Thanks for your help. Yes, generics and many of the System.Collections are not supported in my version of .net.Salzburg
@MandoMando: For a question like this it's probably worth stating which version of .NET you're using, and which version of C#.Torrential
The C# 3 collection initializer is the solution for me because: 1. it compiles on my (special) version of .net 2. does not create a new object. (memory is an issue in this case)Salzburg
Jon, I didn't mention my version of .net somewhat intentionally because it's in beta and changing, and there are very few folks around world that use netmf v4.0.Salzburg
@MandoMando: It would still have been useful information. For example, if you knew you could use C# 3 features even without generics, that would have told us that a collection initializer would be a good way to go.Torrential
@Jon Skeet: Knowing the version info would have saved you time. I honestly cannot make any factual claims about the version of C# supported (it's up to MS .Net team to call that when they're ready). Though I did know that generics are not supported and thought it's unrelated. Based on the responses to my new-to-the-site post, I now know to include all helpful info :) Many thanks for providing solutions for all versions. Which was indeed what I was looking to feed the compiler.Salzburg
G
15

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"});
Gravitate answered 12/11, 2009 at 15:39 Comment(2)
+1 for using List<T> instead or ArrayList (and still answering the original question).Aestivate
Actually, my version of .Net Generics are not supported.Salzburg
K
10

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"};
Knives answered 12/11, 2009 at 15:41 Comment(2)
Excellent! But to compile, the LHS would need to be of type List<string>: private static List<string> alFileTypes = new List<string> {"css", ...Ulrica
@Rubistro, indeed... I just forgot to replace "ArrayList" ;)Knives
G
1
private static System.Collections.ArrayList alFileTypes = 
 new System.Collections.ArrayList(new string [] {"css","gif","htm","html","txt","xml"});
Grimbly answered 12/11, 2009 at 15:38 Comment(0)
U
1

Try

private static System.Collections.ArrayList alFileTypes =   new System.Collections.ArrayList(){"css","gif","htm","html","txt","xml"};
Upholstery answered 12/11, 2009 at 15:40 Comment(1)
I thought this was clever, too. In my mind it didn't even look like it was going to compile :) thanks!Salzburg
M
0

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"});
Minuteman answered 12/11, 2009 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.