C# collection types are designed to store, manage and manipulate similar data more efficiently. Data manipulation includes adding, removing, finding, and inserting data in the collection. Collection types implement the following common functionality:
- Adding and inserting items to a collection
- Removing items from a collection
- Finding, sorting, searching items
- Replacing items
- Copy and clone collections and items
- Capacity and Count properties to find the capacity of the collection and number of items in the collection
.NET supports two types of collections, generic collections and non-generic collections. Prior to .NET 2.0, it was just collections and when generics were added to .NET, generics collections were added as well.
Generic collections with work generic data type. Learn more about generics here: Generics in C#.
The following table lists and matches these classes.
Non-generic Generic
ArrayList -------------> List
HashTable -------------> Dictionary
SortedList -------------> SortedList
Stack -------------> Stack
Queue -------------> Queue
1. Non-Generic
In non-generic collections, each element can represent a value of a different type. The collection size is not fixed. Items from the collection can be added or removed at runtime.
C# ArrayList
ArrayList class is a collection that can be used for any types or objects.
- Arraylist is a class that is similar to an array, but it can be used to store values of various types.
- An Arraylist doesn't have a specific size.
- Any number of elements can be stored.
Output
kiran teja jallepalli
7
10/8/1985 12:00:00 AM
Foreach Loop
It executes for each and every item that exists in the arraylist object. Every time the loop rotates it reads one item from the arraylist and assignes it to the variable.
Note: Arraylist allocates memory for 4 items, whenever an object is created. When a fifth item is added, memory for another 4 items are added. it reduces the memory allocated for the object.
Capacity: is a property that returns the number of items for which memory is allocated.
Here is a detailed tutorial: AarrayList in C#.
C# HashTable
HashTable is similar to arraylist but represents the items as a combination of a key and value.
Output
vb vb.net
asp asp.net
cs cs.net
ora oracle
DictonaryEntry: is a class whose object represents the data in a combination of key & value pairs.
Learn more here: HashTable in C#
C# SortedList
- Is a class that has the combination of arraylist and hashtable.
- Represents the data as a key and value pair.
- Arranges all the items in sorted order.
Output
asp asp.net
cs cs.net
ora oracle
vb vb.net
Learn more here: SortedList in C#.
C# Stack
Output
sqlserver
asp.net
vb.net
cs.net
Here is a detailed tutorial in Stack in C#.
C# Queue
Output
cs.net
vb.net
asp.net
sqlserver
Here is a detailed tutorial: Queue in C#
2. Generic Collections
Generic Collections work on the specific type that is specified in the program whereas non-generic collections work on the object type.
- Specific type
- Array Size is not fixed
- Elements can be added / removed at runtime.
C# List
C# Dictonary
C# SortedList
C# Stack
C# Queue
Summary
This tutorial provides an overview of various collection classes in C#.
No comments:
Post a Comment