Introduction
Today, I bet to throw light on ArrayList in C#. ArrayList has great feature because it can increase and decrease its size dynamically. ArrayList accepts also null value, moreover it allows to add duplicate values.Namespace:
using
System.Collections;
using System;
Here is listed an example how we can add items into
ArrayList.
private void ArraryListInCSharp()
{
ArrayList
slist = new ArrayList();
slist.Add("tc");
slist.Add("C#");
slist.Add("asp.net");
slist.Add("vb.net");
slist.Add("SQL");
if
(slist.Count == 0)
{
MessageBox.Show(slist.Count.ToString());
}
Int16
iCount = Convert.ToInt16(slist.Count
- 1);
for (int i = 0; i <= slist.Count - 1; i++)
{
label1.Text = label1.Text + slist[i].ToString() + ", ";
}
}
In this example below, showing how we can we remove single item from ArrayList.
private void Remove_Item_in_CSharp()
{
//
Declare array
List<int> TC = new List<int>();
//
adding element in arraylist
TC.Add(7);
TC.Add(9);
TC.Add(18);
TC.Add(27);
TC.Add(72);
//
removing item from list
TC.RemoveAll(item => item == 9);
/*
Output would be like as below:
7
18
27
72
*/
}
Here another example in row, how can we remove item from ArrayList in specific range, showing below example gracefully.
private void Remove_Range_In_CSharp()
{
//
Declare array
List<int> TC = new List<int>();
//
adding element in arraylist
TC.Add(7);
TC.Add(9);
TC.Add(18);
TC.Add(27);
TC.Add(72);
//
remove range to remove item in c#
TC.RemoveRange(0, 3);
//
Output
// 72
}
Now finally demonstrating how to add
items in LinkedList as following:
private void Linked_List_In_CSharp()
{
//
Declare Linkedlist
LinkedList<string> TC = new LinkedList<string>();
//
adding element in Linkedlist
TC.AddLast("C#");
TC.AddLast("SilverLight");
TC.AddLast("MVC");
TC.AddFirst("Windows Apps");
//
accessing each item in for loop
foreach (var _itemTC in TC)
{
Console.WriteLine(_itemTC);
}
// ***Output
will be as follow***
//
Windows Apps
// C#
//
SilverLight
// MVC
}
Conclusion:
In above example, I have explained in detail about Add item in ArrayList, Remove single item from ArrayList, remove items range from ArrayList then finally demonstrate about LinkedList.
Post A Comment:
0 comments: