0

I want to store few strings into an array using ArrayList.

How can I store all the strings at once without using Add function every time. Is it somewhat related to interface ICollection in anyway. Can I use ICollection to store my array. If yes How.

ArrayList _1019=new ArrayList("TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN");

I want to store this in the constructor of a class in C#

4

3 に答える 3

8

Well, you can create an array, and populate the ArrayList from that:

ArrayList _1019 = new ArrayList(new string[] { "TEN", "ELEVEN", "TWELVE",
    "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN",
    "NINETEEN" });

That will work with all versions of C#, and all versions of .NET.

As PieterG says, C# 3 has collection initializers - but I suspect that if you're still using ArrayList, you may not be using C# 3.

If you are using C# 3 and targeting anything other than the micro framework, I suggest you use List<string> instead of ArrayList.

于 2010-04-29T07:54:12.203 に答える
4

C# 3.0

ArrayList arrayList = new ArrayList() { "foo", "bar" }; // or
ArrayList arrayList = new ArrayList{ "foo", "bar" };
于 2010-04-29T07:51:19.257 に答える
0

ArrayListを試す必要はありません

string [] stringArray = "red、yellow" .Split( "、")

これは文字列配列を返します

次に、stringArray.ToList()を使用して、文字列の汎用リストを作成できます。

これは、ArrayListと同等の現代的なものになります

于 2010-04-29T07:59:59.560 に答える