collection in c# |
COLLECTIONS IN C#
"Collections
in c# are a dynamic array means it can store multiples items/values as object type like array."
Collection contain of many classes like ArrayList, Hashtable, Stack, Queue, Linkedlist, Sortedlist and many more in .net
Collection contain of many classes like ArrayList, Hashtable, Stack, Queue, Linkedlist, Sortedlist and many more in .net
Why we use Collections instead of Array
The collection is came into
play to eliminate some drawback of an array that's are
1- We cannot
increase the size of array ones it formed means if we want to increase the size
then we can do it by two methods. Either we make a new array and copy the
values form old to new. Or we can do this with the help of Array. Resize
keyword but the drawback is it destroy the existing array and create a new
array by adding the old values into the new one.
2- The
second draw back of an array is we cannot add a value in the middle of an array
it's not possible with an array.
3- Thirds
drawback is we can't delete a value form the middle of an array
But with the help of collection we
can performed the above three task smoothly because it contain a property call auto resizing, that's mean whenever we keep adding the value this size of collection increasing automatically by adding one new box to the dynamic array1. Second we
can add a new value in the middle of a Collection2 and lastly Collection allow us to delete a value from its middle3. Follow the link to see detail with example.
However the Collection contains
multiple classes under the namespace System. Collections such as ArrayList, Hashtable, Stack, Queue, Linkedlist, Sortedlist and many more in .net all these classes
are defined in the library and we just use it. These collections are
Non-Generic Collections
When we came across using the
Collection then the most using collection is the ArrayList
SYNTAX OF COLLECTION (ARRAYLIST)
ArrayList <name> = new ArrayList ( );
After then we can add Object values in the ArrayList
c# collection example |
Example of Arraylist (Collection)
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Console.Write("Enter items/ values = ");
list.Add(Console.ReadLine());
list.Add(100); list.Add(200); list.Add(300);
foreach (object obj in list)
Console.WriteLine(obj);
Console.ReadKey();
}
}
Capacity of ArrayList
In ArrayList there is property
called as Capacity that show you how many items can store in the Collection
(ArrayList), but it is not fixed
early at
the initial stage the capacity will show you 0 zero capacity value because we
do not insert any values yet, but after adding an item the Capacity automatically
change, the capacity of storing item will 4 after adding 4 item it will auto
increase by 8 and will keep the doubling of storing capacity as item reached
the maximum so on, remember it is not fixed you can add item as you want. You
can see the capacity of an ArrayList with the keyword <Name>. Capacity
Example to check Capacity of a Collections
NOTE: Don't forget to add the
directive System.Collections
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Console.
WriteLine ("capacity ="+ list. Capacity); //OUTPUT WILL ZERO
}
Now try
this
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Console.WriteLine(list.Capacity); //OUTPUT WILL ZERO
list.Add(4);
Console.WriteLine(list.Capacity); //OUTPUT WILL 4
Console.ReadKey();
}
In the above examples we use the
default constructor which has 0 initial capacity but we can specify the initial
capacity like this
static void Main(string[] args)
{
ArrayList list = new ArrayList(10);
Console.WriteLine(list.Capacity); //OUTPUT WILL 10
list.Add(4);
Console.WriteLine(list.Capacity); //OUTPUT WILL 10
Console.ReadKey();
}
0 comments:
Post a Comment
Please do not enter any spam link in the comment box