Friday 30 August 2013

Stack example in c#.

Stack:-
It represents a last-in , first out collection of objects.
It is used when you want a last in ,first out access of items. When you add an item in the list, it is called pushing the item and when you remove it , it is called poping the item.

Properties Of  Stack Class:-
Count :- Gets the number of elements actually contained in the Stack.

Methods Of Stack Class :-
There are various methods in Stack class for ex. Add(), Peek(),Remove(),Pop(),Push() etc. I have explained all methods in below example of Stack .

Example:-
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;

namespace CollectionExample
{
    class Program
    {
////Stack Example-------------------------------------------------------------------------
            Console.WriteLine("-------------Stack table Example---------------");
            Stack st = new Stack();

            st.Push('S');
            st.Push('K');
            st.Push('M');
            st.Push('V');
            st.Push('A');

            st.Pop();

            Console.WriteLine("Content Of Stack:");
            foreach (char r in st)
            {
                Console.WriteLine(r);
            }
            st.Push('H');
            st.Push('N');

            Console.WriteLine("The last value in Stack:{0}", st.Peek());
            Console.WriteLine("Current Values in Stack:");
            foreach (char cv in st)
            {
                Console.WriteLine(cv + " ");
            }

            Console.ReadKey();
      }
   }
}

Output:-
-------------Stack table Example---------------
Content Of Stack:
V
M
K
S
The last value in Stack:N
Current Values in Stack:
N
H
V
M
K

S

SortedList example in c#.

SortedList:-
It is the combination of Arraylist and hashTable . It contains a list of items that can be accessed by using a key or an index. If you access items using an index , it is an arraylist ,and if you are accessing an item using a key , it is a hashtable. The collection of items is always sorted by the key value.

Properties Of  SortedList Class:-
Capacity :- Gets or sets the number of elements that SortedList can contain.
Count :- Gets the number of elements actually contained thr SortedList.
IsFixedSize :- Gets a value indicating whether the SortedList has fixed size.
IsReadOnly :-  Gets a value indicating whether the SortedList has ReadOnly.
Item :- Gets or sets the elements at specified index.
Key :- Gets an ICollection containing the keys in the SortedList.
Value :- Gets an ICollection containing the values in the SortedList.

Methods Of SortedList Class :-
There are various methods in SortedList class for ex. Add(), Clear(),Remove() etc. I have explained all methods in below example of sortedlist.

Example:-
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;

namespace CollectionExample
{
    class Program
    {
//SortedList Example-------------------------------------------------------------------------
            Console.WriteLine("-------------SortedList table Example---------------");
            SortedList sl = new SortedList();

            sl.Add("1", "Sushil");
            sl.Add("2", "Kumar");
            sl.Add("3", "Sanjay");
            sl.Add("4", "Muthu");
            sl.Add("5", "Ashu");

            sl.Remove("1");

            if (sl.ContainsValue("Mantra"))
            {
                Console.WriteLine("This name already exist in SortedList.");
            }
            else
            {
                sl.Add("6", "Mantra");
            }

            ICollection key = sl.Keys;// to get collection of key

            foreach (string s in key)
            {
                Console.WriteLine(s + " : " + sl[s]);
            }

            Console.ReadKey();
      }
   }
}

Output:-
-------------SortedList table Example---------------
2 : Kumar
3 : Sanjay
4 : Muthu
5 : Ashu

6 : Mantra

HashTable example in c#.

HashTable:-
A hash table is used when you need to access an element using key. Each item in a hash table has a key/value pair. The key is used to access the item in collection.

Properties Of  HashTable  Class:-
Count :- Gets the number of elements actually contained thr hashtable.
IsFixedSize :- Gets a value indicating whether the hashtable has fixed size.
IsReadOnly :-  Gets a value indicating whether the hashtable has ReadOnly.
Item :- Gets or sets the elements at specified index.
Key :- Gets an ICollection containing the keys in the hashtable.
Value :- Gets an ICollection containing the values in the hashtable.

Methods Of HashTable Class :-
There are various methods in hashtable class for ex. Add(), Clear(),Remove() etc. I have explained all methods in below example of hashtable.

Example:-
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;

namespace CollectionExample
{
    class Program
    {
//HashList Example-------------------------------------------------------------------------
            Console.WriteLine("-------------Hash table Example---------------");
            Hashtable ht = new Hashtable();

            ht.Add("1", "Sushil");
            ht.Add("2", "Kumar");
            ht.Add("3", "Sanjay");
            ht.Add("4", "Muthu");
            ht.Add("5", "Ashu");

            ht.Remove("1");

            if (ht.ContainsValue("Mantra"))
            {
                Console.WriteLine("This name already exist in HashTable.");
            }
            else
            {
                ht.Add("6", "Mantra");
            }

            ICollection key = ht.Keys;// to get collection of key

            foreach (string s in key)
            {
                Console.WriteLine(s + " " + ht[s]);
            }

            Console.ReadKey();
      }
   }
}

Output:-
-------------Hash table Example---------------
3 Sanjay
6 Mantra
4 Muthu
5 Ashu

2 Kumar

Thursday 29 August 2013

ArrayList example in c#.

ArrayList :-

It is an alternative to array. However unlike array you can add and remove items from a list of specified postion using an index and the array resize itself automatically. It allows dynamic memory allocation, Add, search and sort items in the list.

Properties of ArrayList :-
Capacity :- Gets or sets the number of elements that ArrayList can contain.
Count :- Gets the number of elements actually contained thr ArrayList.
IsFixedSize :- Gets a value indicating whether the ArrayList has fixed size.
IsReadOnly :-  Gets a value indicating whether the ArrayList has ReadOnly.
Item :- Gets or sets the elements at specified index.

Methods of ArrayList :-
 There are 15 methods in Arraylist i.e Add(),Clear(),AddRange() which are explained in example. Below example has given in which all methods have used.

Example:-

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;

namespace CollectionExample
{
    class Program
    {
static void Main(string[] args)
        {
            // ArrayList Example ---------------------------------------------------------------
            ArrayList arrayList = new ArrayList();
            Console.WriteLine("Adding some Number.");

            arrayList.Add(12);
            arrayList.Add(34);
            arrayList.Add(54);
            arrayList.Add(23);
            arrayList.Add(65);
            arrayList.Add(33);
            arrayList.Add(89);
            arrayList.Add(39);

            Console.WriteLine("Capacity :{0}", arrayList.Capacity);
            Console.WriteLine("Count :{0}", arrayList.Count);

            Console.Write("Content: ");
            foreach (int i in arrayList)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.Write("Sorted Content: ");
            arrayList.Remove(12);//remove item 12
            arrayList.RemoveAt(1);//remove item from specified position using index
            arrayList.RemoveRange(2, 4);//remove no of item from specified position
            arrayList.Sort();// sorting arrayList
            foreach (int i in arrayList)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

Output:-
Adding some Number.
Capacity :8
Count :7
Content:12 34 54 23 65 33 89 39

Sorted Content: 23 34 

Collection in c#.

Collection provide a more flexible way to work with group of objects. Unlike arrays , the group of objects you work with can grow and shrink  dynamically as the needs of the application change.

Collection is a class , so you must declare a new collection before you can add elements to that collection.

Collection classes are specialized classes for data storage and retrieval. Collection classes provide for stack ,queues,list and hash tables.

Collection classes are various purposes , such as allocating memory dynamically to element and accessing a list of items on the basis of an index etc.

For using the Collection , You have to add System.Collections namespace in your coding.

Types of collection classes and there Usage:-




Class
Description and Usage
ArrayList
It is an alternative to array. However unlike array you can add and remove items from a list of specified postion using an index and the array resize itself automatically. It allows dynamic memory allocation,
Add, search and sort items in the list.
HashTable
A hash table is used when you need to access an element using key. Each item in a hash table has a key/value pair. The key is used to access the item in collection.
SortedList
It is the combination of Arraylist and hashTable . It contains a list of items that can be accessed by using a key or an index. If you access items using an index , it is an arraylist ,and if you are accessing an item using a key , it is a hashtable. The collection of items is always sorted by the key value.
Stack
It represents a last-in , first out collection of objects.
It is used when you want a last in ,first out access of items. When you add an item in the list , it is called pushing the item and when you remove it , it is called poping the item.
Queue
It represents a first in, first out collection of object.
It is used when you want a first in, first out access of items. When you add an item in the list , it is called enqueue and when you remove an item , it is called dequeue.
BitArray
It is represent an array of the binary representation using the values 1 and 10.
It is used when you want to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index which start from 0(Zero).

What is MVC in Asp.net.

The Model-View-Controller architectural pattern separates an appliaction into three component : The Model,The View and The Controller.The Asp.Net MVC framework is an alternative to the Asp.Net web form pattern for creating MVC based web application.
It is a light weight framework, highly testable presentation framework that is integrated with existing Asp.Net feature, such as master page and form authentiactions.The MVC framework is defined in System.Web.Mvc namespace.

Understanding  Model:-
An MVC model contains all of your application logic that are not contained in Views or Controller. The Model contains all of business logic, database access logic and validation logic.
For example, If you are using Microsoft Entity Framework to access your database, then you would create your entity framework class (.edmx file) in the model folder.

Understanding  View:-
A view contain html markup and content that is sent to the browser. A view is equivalent to the page when working with an asp.net MVC.

Understanding  Controller:-
A controller is responsible for controlling the way user interacts with MVC appliaction. A controller contains the flow control logic for MVC appliaction. A controller determines what reponse to send back to the user when a user make a browser request.
Controller is just  a class(.cs file).