C# - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C# Collections

C# Collections

shape Description

C# Collections is a class used to manage group of related objects and which are enhancement to arrays. Using collection classes data storage and retrieval is easily possible. Collection provide a way to work with group of object and allocating the memory dynamically. .Net frame work provide many collection,to use a collection class it should be declared and add element to the collection group. It create collection of object of type Object class which is a base class of every type i.e System.Object. Collections can be categorized in to two types • System.CollectionsSystem.Collections.Generics(.Net Framework 2.0)

Stack

shape Description

A stack is a Last-In-First-Out (LIFO) data structure. It is used to access item from-in last is first out manner. The stack is used when implementing calculators. • When an item is added to array it is called PUSH Operation. • When an item is removed from array it is called as POP Operation. Properties: Count

shape Example

[csharp] using System; using System.Collections.Generic; using System.IO; using System.Collections; namespace SPLessons { class Program { static void Main(string[] args) //Main class start here { Stack<int> stack = new Stack<int>(); //Stack Decleration stack.Push(8); //Push Operation stack.Push(4); stack.Push(6); stack.Push(2); stack.Push(1); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Peek()); Console.WriteLine(stack.Peek()); Console.WriteLine(); foreach (int item in stack) { Console.WriteLine(item); //Print the stack } } } } [/csharp] Output:

Queue

shape Description

A queue is a First-In-First-Out (FIFO) data structure. It is used to access item from list is- first in and first out manner. Queues may be used to process messages as they appear or serve customers as they come. • When an item is added to list it is called Enqueue • When an item is removed from list it is called Dequeue Properties: Count

shape Example

[csharp] using System; using System.Collections.Generic; using System.IO; using System.Collections; namespace SPLessons { class Program { static void Main(string[] args) { Queue<string> msgs = new Queue<string>(); msgs.Enqueue("Hello"); msgs.Enqueue("Welcome"); msgs.Enqueue("SPlessons"); msgs.Enqueue("C#"); msgs.Enqueue("Tutorial"); Console.WriteLine(msgs.Dequeue()); Console.WriteLine(msgs.Peek()); Console.WriteLine(); foreach (string msg in msgs) { Console.WriteLine(msg); } } } } [/csharp] Output:

Array List

shape Description

Array List is ordered collection of elements that implements the IList interface which allow searching and sorting of items. Using an array, whose size is dynamically increased as required random access to its element is possible. To overcome the disadvantages of array, Array List is implemented which allow add and remove of item from list at a particular point using index. Properties: Capacity, Count, Item, IsReadOnly , IsFixedSize Limitations:The flexibility of ArrayList comes at a cost of performance. Since memory allocation is a very expensive business, the fixed number of elements of the simple array makes it much faster to work with.

shape Example

[csharp] using System; using System.Collections.Generic; using System.IO; using System.Collections; namespace SPLessons { class Program { class Empty{ } static void Main(string[] args) { ArrayList da = new ArrayList(); da.Add("SPLessons-C# Tutorial"); da.Add(12); da.Add(100); da.Add(new Empty()); da.Remove(100); foreach(object el in da) { Console.WriteLine(el); } } } } [/csharp] Output:

Hast Tables

shape Description

Hash table objects are quite similar to Array List Object. It is a collection of key-value pair i.e(Key,Value) which are arranged according to key .It computes a hash for each key you add and look for hash code. Methods: Add, Clear, Clone, Contain, ContainKey, Equals,Finalize, GetEnumerator, GetHash, GetHashCode, GetObjectData, GetType, KeyEquals, Remove, ToString Properties: compare, Count,EqualityComparer, hcp, IsFixedSize, IsReadOnly, Item, Key, Values, IsSynchronised Disadvantages: It is slow when compared to Generic Dictionary. Performance: Although the lookup is very quick in a Hashtable, the CLR must do quite a lot of work to maintain them that is very resource-intensive.

shape Syntax

Hashtable obj = new Hashtable();

shape Example

[csharp] using System; using System.Collections.Generic; using System.IO; using System.Collections; namespace SPLessons { class Program { static void Main(string[] args) { Hashtable hash = new Hashtable(); //Adding elements hash[Convert.ToDateTime("02/14/2015")] = "Children day"; hash[Convert.ToDateTime("09/05/2015")] = "Teachers day"; hash[Convert.ToDateTime("08/15/1947")] = "Independence day"; //input date from user Console.WriteLine("Please Enter date in 'Month/Date/Year' Format"); DateTime DateData = DateTime.Parse(Console.ReadLine()); Console.WriteLine(Hash[DateData]); Console.WriteLine("Press any key"); Console.ReadKey(); } } } [/csharp] Output:

Dictionary

shape Description

A dictionary, also called an associative array, which provide fast lookup based on Keys. It is a collection of unique keys and a collection of values, where each key is associated with one value. Retrieving and adding values is very fast. Dictionaries take more memory, because for each value there is also a key. Methods: Add, Clear, ContainsKey, ContainsValue, Equals, Finalize, GetHashCode, GetType, GetEnumerator, MemberwiseClone, Remove, ToString, TryGetValue Properties: Compare,Count,Item,Keys,Values

shape Example

[csharp] using System; using System.Collections.Generic; using System.IO; using System.Collections; namespace SPLessons { class Program { static void Main(string[] args) { Dictionary<string, string> domains = new Dictionary<string, string>(); domains.Add("FB", "Football"); domains.Add("VB", "VolleyBall"); Console.WriteLine(domains["FB"]); Console.WriteLine(domains["VB"]); Console.WriteLine("Dictionary has {0} items", domains.Count); Console.WriteLine("Keys of the dictionary:"); List<string> keys = new List<string>(domains.Keys); foreach (string key in keys) { Console.WriteLine("{0}", key); } Console.WriteLine("Values of the dictionary:"); List<string> vals = new List<string>(domains.Values); foreach (string val in vals) { Console.WriteLine("{0}", val); } Console.WriteLine("Keys and values of the dictionary:"); foreach (KeyValuePair<string, string> kvp in domains) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } } } } [/csharp] Output: