GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C# - Collections
Selected Reading
C# - CollectionsCollection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces. Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc. These classes create collections of objects of the Object class, which is the base class for all data types in C#. C# provides various collection classes, the important collection classes are:
ArrayList CollectionAn ArrayList is a non-generic collection that can store elements of any data type. It resizes dynamically based on the number of elements added. ExampleThis example demonstrates how to use an ArrayList to store and display elements of different types: using System;
using System.Collections;
class Program
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add("Hello");
list.Add(3.14);
foreach (var item in list)
{
Console.WriteLine(item);
}
}
}
When the above code is compiled and executed, it produces the following result − 1 Hello 3.14 Hashtable CollectionsA Hashtable is a non-generic collection which stores key-value pairs. ExampleThis example demonstrates how to create a Hashtable and retrieve values using keys: using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable.Add("ID", 101);
hashtable.Add("Name", "Sudhir");
hashtable.Add("Age", 30);
Console.WriteLine($"ID: {hashtable["ID"]}");
Console.WriteLine($"Name: {hashtable["Name"]}");
Console.WriteLine($"Age: {hashtable["Age"]}");
}
}
When the above code is compiled and executed, it produces the following result − ID: 101 Name: Sudhir Age: 30 SortedList CollectionsA SortedList is a collection of key-value pairs that are automatically sorted by the keys. ExampleThis example demonstrates how a SortedList stores and displays elements in sorted order of keys: using System;
using System.Collections;
class Program
{
static void Main()
{
SortedList sortedList = new SortedList();
sortedList.Add(3, "Three");
sortedList.Add(1, "One");
sortedList.Add(2, "Two");
foreach (DictionaryEntry item in sortedList)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
}
}
When the above code is compiled and executed, it produces the following result − 1: One 2: Two 3: Three Stack CollectionsA Stack collection follows last-in, first-out (LIFO) approach that means the items are added to and removed from the top of the stack. ExampleThis example demonstrates how to push items onto a stack and pop them in reverse order: using System;
using System.Collections;
class Program
{
static void Main()
{
Stack stack = new Stack();
stack.Push("First");
stack.Push("Second");
stack.Push("Third");
while (stack.Count > 0)
{
Console.WriteLine(stack.Pop());
}
}
}
When the above code is compiled and executed, it produces the following result − Third Second First Queue CollectionsA Queue collection follows first-in, first-out (FIFO) approach that means the items are enqueued (added) at the end and dequeued (removed) from the front. ExampleThis example demonstrates how to enqueue items into a queue and dequeue them in the same order: using System;
using System.Collections;
class Program
{
static void Main()
{
Queue queue = new Queue();
queue.Enqueue("First");
queue.Enqueue("Second");
queue.Enqueue("Third");
while (queue.Count > 0)
{
Console.WriteLine(queue.Dequeue());
}
}
}
When the above code is compiled and executed, it produces the following result − First Second Third Various Collection Classes and Their UsageThe following are the various commonly used classes of the System.Collection namespace. Click the following links to check their detail.
Advertisements
| ||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |