6

How to use ArrayList to store data using ICollection

 2 years ago
source link: https://www.codesd.com/item/how-to-use-arraylist-to-store-data-using-icollection.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to use ArrayList to store data using ICollection

advertisements

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#


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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK