Collections in C#.Net

Collection:

    Collections are used to store complex and mixed datatypes.

NameSpace:

using system.collections;

Types Of Collections:

1.ArrayList:

    It is used to store complex and mixed datatypes

Example 1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace arraylistexample
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList alist = new ArrayList();
            alist.Add("Venu");
            alist.Add("Gopal");
            alist.Add("Krishna");
            alist.Add(2);
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
}

OutPut:

Venu
Gopal
Krishna
2

Example 2:Merge Two arraylists

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace arraylistexample
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList alist = new ArrayList() {1,2,3};
            ArrayList alist2 = new ArrayList() { 4, 5, 6 };
            alist.AddRange(alist2);
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
}

OutPut:

1
2
3
4
5
6

Example3:Merge Two ArrayLists

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace arraylistexample
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList alist = new ArrayList();
            alist.Add("Venu");
            alist.Add("Gopal");
            ArrayList alist2 = new ArrayList();
            alist2.Add("Krishna");
            alist.AddRange(alist2);
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }

            Console.Read();
        }
    }
}

OutPut:

Venu
Gopal
Krishna

2.SortedList:

   It is used to store data in key/value pair, By using sorted list we will sort the data.

Example1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace sortedlistexp
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedList<int, string> slist = new SortedList<int, string>();
            slist.Add(1,"Venu");
            slist.Add(2,"Gopal");
            slist.Add(3,"Krishna");
            foreach(var item in slist)
            {
                Console.WriteLine("Key Is:"+item.Key);
                Console.WriteLine("Value Is:"+item.Value.ToString());
            }
            Console.Read();
        }
    }
}

OutPut:

Key Is:1
Value Is:Venu
Key Is:2
Value Is:Gopal
Key Is:3
Value Is:Krishna

3.HashTable:

       It is used to store data in key/value pair and for every hash key hash code will be generated by using this code we will get the data easily and fast.

Example:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace hashtableexp
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable htable = new Hashtable();
            htable.Add(1, "Venu");
            htable.Add(2, "Gopal");
            htable.Add(3, "Krishna");
            foreach (DictionaryEntry entry in htable)
            {
                Console.WriteLine("Key Is:"+entry.Key);
                Console.WriteLine("Value Is:" + entry.Value.ToString());
            }
            Console.Read();
        }
    }
}

OutPut:


Key Is:3
Value Is:Krishna
Key Is:2
Value Is:Gopal
Key Is:1
Value Is:Venu