Friday, December 3, 2021

What are Collections in C#

 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. 

  1. Arraylist is a class that is similar to an array, but it can be used to store values of various types.
  2. An Arraylist doesn't have a specific size.
  3. Any number of elements can be stored.
using System.Collections;

protected void Button1_Click(object sender, EventArgs e)
{
     ArrayList al = new ArrayList();
     string str = "kiran teja jallepalli";
     int x = 7;
     DateTime d = DateTime.Parse("8-oct-1985");
     al.Add(str);
     al.Add(x);
     al.Add(d);

     foreach (object o in al)
     {
        Response.Write(o);
        Response.Write("<br>");
     }
}
C#

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.

using System.Collections;

protected void Button2_Click(object sender, EventArgs e)
{
    Hashtable ht = new Hashtable();
    ht.Add("ora", "oracle");
    ht.Add("vb", "vb.net");
    ht.Add("cs", "cs.net");
    ht.Add("asp", "asp.net");

    foreach (DictionaryEntry d in ht)
    {
       Response.Write (d.Key + " " + d.Value);
       Response.Write("<br>");
    }
}
C#

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

  1. Is a class that has the combination of arraylist and hashtable.
  2. Represents the data as a key and value pair.
  3. Arranges all the items in sorted order. 
using System.Collections;

protected void Button3_Click(object sender, EventArgs e)
{
     SortedList sl = new SortedList();
     sl.Add("ora", "oracle");
     sl.Add("vb", "vb.net");
     sl.Add("cs", "cs.net");
     sl.Add("asp", "asp.net");

     foreach (DictionaryEntry d in sl)
     {
         Response.Write(d.Key + " " + d.Value);
         Response.Write("<br>");
     }
}
C#

Output

asp  asp.net
cs   cs.net
ora  oracle
vb   vb.net

Learn more here: SortedList in C#. 

C# Stack

protected void Button4_Click(object sender, EventArgs e)
{
    Stack stk = new Stack();
    stk.Push("cs.net");
    stk.Push("vb.net");
    stk.Push("asp.net");
    stk.Push("sqlserver");

    foreach (object o in stk)
    {
       Response.Write(o + "<br>");
    }
}
C#

Output

sqlserver
asp.net
vb.net
cs.net

Here is a detailed tutorial in Stack in C#. 

C# Queue

using System.Collections;

protected void Button5_Click(object sender, EventArgs e)
{
    Queue q = new Queue();
    q.Enqueue("cs.net");
    q.Enqueue("vb.net");
    q.Enqueue("asp.net");
    q.Enqueue("sqlserver");

    foreach (object o in q)
    {
       Response.Write(o + "<br>");
    }
}
C#

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. 

  1. Specific type
  2. Array Size is not fixed
  3. Elements can be added / removed at runtime.

C# List

using System.Collections.Generic;

protected void Button1_Click(object sender, EventArgs e)
{
    List<int> lst = new List<int>();
    lst.Add(100);
    lst.Add(200);
    lst.Add(300);
    lst.Add(400);
    foreach (int i in lst)
    {
        Response.Write(i+"<br>");
    }
}
C#

C# Dictonary

using System.Collections.Generic;

protected void Button1_Click(object sender, EventArgs e)
{
    Dictionary<int, string> dct = new Dictionary<int, string>();
    dct.Add(1, "cs.net");
    dct.Add(2, "vb.net");
    dct.Add(3, "vb.net");
    dct.Add(4, "vb.net");
    foreach (KeyValuePair<int, string> kvp in dct)
    {
        Response.Write(kvp.Key + " " + kvp.Value);
        Response.Write("<br>");
    }
}
C#

C# SortedList

using System.Collections.Generic;

protected void Button3_Click(object sender, EventArgs e)
{
    SortedList<string, string> sl = new SortedList<string, string>();
    sl.Add("ora", "oracle");
    sl.Add("vb", "vb.net");
    sl.Add("cs", "cs.net");
    sl.Add("asp", "asp.net");

    foreach (KeyValuePair<string, string> kvp in sl)
    {
        Response.Write(kvp.Key + " " + kvp.Value);
        Response.Write("<br>");
    }
}
C#

C# Stack

using System.Collections.Generic;

protected void Button4_Click(object sender, EventArgs e)
{
    Stack<string> stk = new Stack<string>();
    stk.Push("cs.net");
    stk.Push("vb.net");
    stk.Push("asp.net");
    stk.Push("sqlserver");

    foreach (string s in stk)
    {
        Response.Write(s + "<br>");
    }
}
C#

C# Queue

using System.Collections.Generic;
protected void Button1_Click(object sender, EventArgs e)
{
    Queue<string> q = new Queue<string>();

    q.Enqueue("cs.net");
    q.Enqueue("vb.net");
    q.Enqueue("asp.net");
    q.Enqueue("sqlserver");

    foreach (string s in q)
    {
        Response.Write(s + "<br>");
    }
}
C#

Summary

This tutorial provides an overview of various collection classes in C#.  

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...