You are on page 1of 17

Points to be Discussed

Namespaces

Meaning and its Working Using System Namespace and Object Class Using System. Collections Array List Stack Queue Sorted List

Namespaces

Namespaces
Namespace is a collection of classes. Namespaces

provide re-usability of existing classes. It can be compared to a folder and the classes can be compared to the files. Namespaces are declared with the C# namespace keyword. Classes that are two be included in the namespace must be declared within the namespaces curly brackets. namespace MyClassses { class MyFirstClass { }

Members of Namespace
The following types of declarations can appear in

a namespace: 1. Classes 2. Structures 3. Interfaces 4. Enumerations 5. Delegates

Using the using keyword


Using the using keyword with a namespace tells

the C# compiler that we want to refer to classes in the named namespace without prefixing the class names with the namespace name. Exusing System; This is called a namespace directive. Namespace directives tell the C# compiler that the code will be using classes from the namespace and that the classes wont be prefixed the namespace name. The C# compiler does the work of finding the definition of each class in one of the namespaces referenced in a namespace directive.

System Namespace
The System namespace contain classes that

implement basic functionality, such as data type conversions, mathematical operations, program invocation and process environment management.

Using System.Collections

ArrayList
An ArrayList is an array that can dynamically grow and shrink. Properties: Capacity: Gets or sets the number of elements that the ArrayList can contain Count: Gets the number of elements contained in an Array List object IsFixedSize: Gets a value indicating whether a ArrayList object has a fixed size. Item: Gets or sets the element at the specified index

ArrayList
Methods: Add(value): Adds an object to the end of the ArrayList . ArrayList is one of the most flexible data structure from C# Collections. ArrayList contains a simple list of values.

private void button1_Click(object sender, EventArgs e) { int i = 0; ArrayList ItemList = new ArrayList(); ItemList.Add("Item4"); ItemList.Add("Item5"); ItemList.Add("Item2"); ItemList.Add("Item1"); ItemList.Add("Item3"); MessageBox.Show ("Shows Added Items"); for (i=0; i<=ItemList.Count-1;i++) { MessageBox.Show(ItemList[i].ToString()); } ItemList.Insert(3, "Item6"); ItemList.Sort(); ItemList.Remove("Item1"); ItemList.RemoveAt(3); MessageBox.Show("Shows final Items the ArrayList"); for (i=0; i<=ItemList.Count-1;i++) { MessageBox.Show(ItemList[i].ToString()); }}

Stack
The Stack class represents a last-in-first-out

(LIFO) Stack of Objects. Stack follows the pushpop operations. That is we can Push (insert) Items into Stack and Pop (retrieve) it back . In Stack, we can push the items into a stack and get it in reverse order. Stack returns the last item first. Common methods used in Stack are: 1. Push 2. Pop

private void button1_Click(object sender, EventArgs e) { Stack days = new Stack(); days.Push("SunDay"); days.Push("MonDay"); days.Push("TueDay"); days.Push("WedDay"); days.Push("ThuDay"); days.Push("FriDay"); days.Push("SaturDay"); if (days.Count ==7) { MessageBox.Show(days.Pop().ToString ()); } else { MessageBox.Show("SaturDay does not exist"); } }

Queue
The Queue works like FIFO system , a first-in,

first-out collection of Objects. Objects stored in a Queue are inserted at one end and removed from the other. We can Enqueue items in Queue and we can Dequeue or we can Peek (that is we will get the reference of first item ) item from Queue. Common methods used in Queue are: 1. Enqueue 2. Dequeue 3. Peek

private void button1_Click(object sender, EventArgs e) { Queue days = new Queue(); days.Enqueue("Sunday"); days.Enqueue("Monday"); days.Enqueue("Tuesday"); days.Enqueue("Wednesday"); days.Enqueue("Thursday"); days.Enqueue("Friday"); days.Enqueue("Saturday"); MessageBox.Show (days.Dequeue().ToString ()); if (days.Contains("Monday")) { MessageBox.Show("The queue contains Monday"); } else { MessageBox.Show("Does not match any entries"); } }

Sorted List
The SortedList class represents a collection of key-

and-value pairs that are sorted by the keys and are accessible by key and by index.
Properties: Capacity: Gets or sets the capacity of a SortedList object Count: Gets the number of elements contained in a SortedList object IsFixedSize: Gets a value indicating whether a SortedList object has a fixed size.

Sorted List
4. Item: Gets and sets the value associated with a specific key in a SortedList object 5. Keys: Gets the keys in a SortedList object 6. Values: Gets the values in a SortedList object Methods: 1. Add(key,value): Adds an element with the specified key and value to a SortedList object.

public class SortedList { SortedList mySortedList = new SortedList(); public static void Main() { mySortedList.Add("NY", "New York"); mySortedList.Add("FL", "Florida"); mySortedList.Add("AL", "Alabama"); mySortedList.Add("WY", "Wyoming"); mySortedList.Add("CA", "California"); foreach (string myKey in mySortedList.Keys) { Console.WriteLine("myKey = " + myKey); } foreach (string myValue in mySortedList.Values) { Console.WriteLine("myValue = " + myValue); } if (mySortedList.ContainsKey("FL")) if (mySortedList.ContainsValue("Florida")) mySortedList.Remove("FL");

You might also like