how to create and add items into the ArrayList in csharp
ArrayList is one of the most flexible data structure available in .net collections. ArrayList contains a simple list of values. We can easily add, insert and delete items in an ArrayList without worrying about the size. ArrayList manages to grow and shrink automatically whenever we add or delete items from it.
In this exercise, we will add data - customers and iterate through the ArrayList.
Create a Windows Form and place the following code in the Form_Load event.
The following statement shows how to create a new instance of an ArrayList class.
Once ArrayList class is instantiated, we can add items to it using the Add method. Click here to know How to add items to the ArrayList using an Add method. When you run this program five customer details are added to the arraylist and displayed.
The below code displays ArrayList data on a new line.
As we have taken Windows Form as an example, we will see how to display ArrayList data into the textbox controls of a windows form.
ArrayList stores data as object type and so, we need cast it to a string type or by using ToString() method and assign to the Textbox controls as shown below.
In this exercise, we will add data - customers and iterate through the ArrayList.
Create a Windows Form and place the following code in the Form_Load event.
private void Form1_Load(object sender, EventArgs e) { ArrayList arrayList = new ArrayList(); arrayList.Add("Customer1"); arrayList.Add("Customer2"); arrayList.Add("Customer3"); arrayList.Add("Customer4"); arrayList.Add("Customer5"); //Iterating through items - Using foreach loop string str = string.Empty; foreach (string strName in arrayList) { str += strName + "\n"; } MessageBox.Show(str); }
The following statement shows how to create a new instance of an ArrayList class.
ArrayList arrayList = new ArrayList();
Once ArrayList class is instantiated, we can add items to it using the Add method. Click here to know How to add items to the ArrayList using an Add method. When you run this program five customer details are added to the arraylist and displayed.
The below code displays ArrayList data on a new line.
foreach (string strName in arrayList) { str += strName + "\n"; }
As we have taken Windows Form as an example, we will see how to display ArrayList data into the textbox controls of a windows form.
ArrayList stores data as object type and so, we need cast it to a string type or by using ToString() method and assign to the Textbox controls as shown below.
txtName.Text = (string) arrayList[0]; or txtName.Text = arrayList[1].ToString();
ArrayList Articles
- difference between arraylist and list collection
- Arrays vs ArrayList
- how to add items to the arraylist using an add method
- retrieve arraylist items using enumerators
- retrieve arraylist items using for each loop
- retrieve arraylist items using indexers
- insert items to the arraylist using insert and insertrange method
- remove items from arraylist
- find item in arraylist using indexof method
- bind arraylist with gridview control in asp net application
- how to bind arraylist with dropdownlist in asp net application
- how to bind arraylist with radiobuttonlist in asp.net application
- convert arraylist to string array
- convert string array to arraylist
- convert datatable into an arraylist
- convert dataset to arraylist in asp.net using c#
- how to retrieve arraylist items in reverse order
Most Viewed

Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls