Collection and Arrays in .Net

Collections and arrays in .net. Here you find discussion on arrays, arraylist, stack, queue, dictionaries, hashtable, sortedlist, string and stringbuilder, etc.

Difference Between String and StringBuilder

String Class StringBuilder
Once the string object is created, its length and content cannot be modified. Even after object is created, it can be able to modify length and content.
Slower Faster

Example of Single Dimension Array in .Net

int [] intArray = new int[3];

intArray[2] = 22; // set the third element to 22

Example of Multi Dimension Array in .Net

int [][] myTable = new int[2,3];

myTable[1,2] = 22;

Example of Jagged Array in .Net : Variable Length Array in .Net

int [][] myTable = new int[3][];

myTable[0] = new int[5];

myTable[1] = new int[2];

myTable[2] = new int[4];

myTable[0][2] = 11;

Example of String Array in .Net

string []names = new string[4];

names[2] = “God will make me win”;

Limitation of Arrays

  • The size of an array is always fixed and must be defined at the time of instantiation of an array.
  • Secondly, an array can only contain objects of the same data type, which we need to define at the time of its instantiation.

Collections in .Net : Namespace for Collections in .Net

System.Collections namespace

ArrayList Concept in .Net

Provides a collection similar to an array, but that grows dynamically as
the number of elements change.

Example

static void Main()
{
    ArrayList list = new ArrayList();
    list.Add(11);
    list.Add(22);
    list.Add(33);
    foreach(int num in list)
    {
        Console.WriteLine(num);
    }
}

Output

11 22 33


Stack Concept in .Net

A collection that works on the Last In First Out (LIFO) principle,
i.e., the last item inserted is the first item removed from the collection.
Push - To add element and
Pop – To Remove element

Example

using System;
using System.Collections;

class Test
{
    static void Main()
    {
        Stack stack = new Stack();
        stack.Push(2);
        stack.Push(4);
        stack.Push(6);

        while(stack.Count != 0)
        {
            Console.WriteLine(stack.Pop());
        }
    }
}

Output
6 4 2


Queue Concept in .Net

A collection that works on the First In First Out (FIFO) principle, i.e.,
the first item inserted is the first item removed from the collection.
Enqueue - To add element and Dequeue – To Remove element
Example:

static void Main()
{
    Queue queue = new Queue();
    queue.Enqueue(2);
    queue.Enqueue(4);
    queue.Enqueue(6);

    while(queue.Count != 0)
    {
        Console.WriteLine(queue.Dequeue());
    }
}

Output
2
4
6


Dictionaries Concept in .Net

Dictionaries are a kind of collection that store items in a key-value pair fashion.

System.Collections namespace


Hashtable Concept in .Net

Provides a collection of key-value pairs that are organized
based on the hash code of the key.

Example:
static void Main()
{
    Hashtable ht =  new Hashtable(20);
    ht.Add("ht01", "DotNetGuts");
    ht.Add("ht02", "EasyTutor.2ya.com");
    ht.Add("ht03", "DailyFreeCode.com");

    Console.WriteLine("Printing Keys...");
    foreach(string key in ht.Keys)
    {
        Console.WriteLine(key);
    }

    Console.WriteLine("\nPrinting Values...");
    foreach(string Value in ht.Values)
    {
        Console.WriteLine(Value);
    }

    Console.WriteLine("Size of Hashtable is {0}", ht.Count);

    Console.WriteLine(ht.ContainsKey("ht01"));
    Console.WriteLine(ht.ContainsValue("DailyFreeCode.com"));

    Console.WriteLine("\nRemoving element with key = ht02");
    ht.Remove("ht02");

    Console.WriteLine("Size of Hashtable is {0}", ht.Count);

}

Output

Printing Keys…
ht01
ht02
ht03

Printing Values…
DotNetGuts
EasyTutor.2ya.com
DailyFreeCode.com

Size of Hashtable is 3
True
True

Removing element with key = ht02
Size of Hashtable is 2


SortedList Concept in .Net

Provides a collection of key-value pairs where the items are sorted according to the key. The items are accessible by both the keys and the index.
Example:

static void Main()
{
SortedList sl = new SortedList();
sl.Add(18, "Java");
sl.Add(5, "C#");
sl.Add(11, "VB.Net");
sl.Add(1, "C++.Net");

Console.WriteLine(“The items in the sorted order are…”);
Console.WriteLine(“\t Key \t\t Value”);
Console.WriteLine(“\t === \t\t =====”);
for(int i=0; i<sl.Count; i++)
{
Console.WriteLine(“\t {0} \t\t {1}”, sl.GetKey(i),
sl.GetByIndex(i));
}
}

Output

Key Value

==
1 C++.Net
5 C#
11 VB.Net
18 Java

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: