COLLECTIONS IN C PRESENTATION CONTENT LAKSHMI MAREDDY CIS














![2. 6 IENUMERATOR String names[]=new String[2] {”Rodney”, ”Stutte”, ”Cass”}; for(IEnumerator e =names. Get. Enumerator(); 2. 6 IENUMERATOR String names[]=new String[2] {”Rodney”, ”Stutte”, ”Cass”}; for(IEnumerator e =names. Get. Enumerator();](https://slidetodoc.com/presentation_image_h2/4046ffe271e17afbb28bd28db4f1ec06/image-15.jpg)











- Slides: 26

COLLECTIONS IN C# PRESENTATION CONTENT LAKSHMI MAREDDY CIS 525, BELLEVUE UNIVERSITY, NE INSTRUCTOR: PROF. STUTTE 1. Concepts Recap 2. What is a collection? 3. Types of Collection 4. Example 5. References

1. CONCEPTS RECAP Stacks and Heaps Value and reference Interfaces 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 2

1. 1 STACK AND HEAP MEMORY TEMPORARY MEMORY STACK Stores variable types in address' in memory, these variables in programming are called local variables and are often stored for short amounts of time while a function/method block uses them to compute a task. HEAP Contains • attributes, • constructors and • methods of a class / object Invoke a method in a heap Method’s (function’s) variables etc. are also stored in stack, but not in heap, and destroyed after use. Heap also sits in stack 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 3

1. 2 VALUE & TYPE Point my. Point = new Point (0, 0); Form my. Form = new Form(); Test (ref my. Point, ref my. Form); void Test (ref Point p, ref Form f) { p. X = 100; f. Text = “Hello, World!”; f = null; } 12/31/2021 // a new value-type variable // a new reference-type variable // pass my. Point and my. Form by reference // This will change my. Point’s position // This will change My. Form’s caption // This will nuke the my. Form variable! Lakshmi Mareddy CIS 525 Bellevue University NE 4

1. 2 SYSTEM INTERFACES IN C# An interface contains only the signatures of interface ISample. Interface { void Sample. Method(); } methods, delegates or events. class Implementation. Class : ISample. Interface { The implementation of the { methods is done in the class that implements the interface } Defining an interface offers a new dimension of flexibility (Anyone implementing the interface can change the way the members are coded. ) while keeping things standard so code will still be uniform. This, in turn, provides a guarantee that the code won't break when new methods are coded against the same interface. // Method implementation. static void Main() { } 12/31/2021 // Explicit interface member implementation: void ISample. Interface. Sample. Method() } // Declare an interface instance. ISample. Interface obj = new Implementation. Class(); // Call the member. obj. Sample. Method(); Lakshmi Mareddy CIS 525 Bellevue University NE 5

2. WHAT IS A COLLECTION? Collections are enumerable data structures that can be assessed using indexes or keys. Closely related data can be handled more efficiently when grouped together into a collection. In plain English, it is a list, with an index, to access the list. Instead of writing separate code to handle each individual object, you can use the same code to process all the elements of a collection. In programming terms, we are looking at an array, which has an index, and can be accessed via the index 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 6

2. 1 SYSTEM NAMESPACE Basic Interfaces i. Collection i. Enumerator i. List i. Enumerable i. Dictionary 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 7

2. 2 ICOLLECTION i. Collection 12/31/2021 System. Collections. Stack System. Collections. Queue System. Collections. Bit. Array System. Collections. Specialized. Name. Value. Collection Lakshmi Mareddy CIS 525 Bellevue University NE 8

2. 2. 1 QUEUE The Queue is a data structure that provides a First-in-First-Out collection of items of the System. Object type. The Enqueue() method is responsible for storing items at the rear of the Queue The Dequeue() removes them one at a time from the front of the Queue 12/31/2021 using System; using System. Collections; class Test { static void Main() { Queue queue. Object = new Queue(); queue. Object. Enqueue(“Peter"); queue. Object. Enqueue(“Tony"); queue. Object. Enqueue(“Ralph"); while (queue. Object. Count > 0) Console. Write. Line(queue. Object. Dequeue()) ; Console. Read. Line(); } } Lakshmi Mareddy CIS 525 Bellevue University NE 9

2. 3 ILIST i. List System. Array System. Collections. Array. List System. Collections. Specialized. String. Collection The i. List interface represents collections that only have value. 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 10

2. 3. 1 ARRAYLIST EXAMPLE The initial capacity of an Array. List is 16, which is increased once the 17 th item is stored onto it. This repeated memory allocation and copying of the items can be quite expensive in some situations. If we explicitly set the initial size, then we can improve performance. 12/31/2021 using System; using System. Collections; class Test { static void Main() { int i = 100; double d = 20. 5; Array. List array. List = new Array. List(); array. List. Capacity = 2; array. List. Add(“Peter"); array. List. Add(i); array. List. Add(d); for (int index = 0; index <array. List. Count; index++) Console. Write. Line(array. List[index]); } } Lakshmi Mareddy CIS 525 Bellevue University NE 11

2. 4 IDICTIONARY i. Dictionary System. Collections. Sorted. List System. Collections. Hashtable System. Collections. Specialized. Hybrid. Dictionary System. Collections. Specialized. List. Dictionary The i. Dictionary interface represents collections that have name value pairs. A Code 0 Obj X 12/31/2021 Lives at #22, Wisteria Lane Is color Red Index to another collection Looks at Address Book List (Home) Lakshmi Mareddy CIS 525 Bellevue University NE 12

2. 4. 1 EXAMPLE using System; using System. Collections. Specialized; class Test { static void Main() { String. Dictionary string. List = new. String. Dictionary(); string. List. Add("A", “Ralph"); string. List. Add("B", “China"); string. List. Add("C", "Jini"); string. List. Add("D", “Evan"); foreach (string str in string. List. Values) Similar to the String. Collection class. String. Dictionary class, is a Hashtable that has its keys as strings only. A Hashtable can contain any object type in its key. { Console. Write. Line(str); } } } 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 13

2. 5 IENUMERATOR Enumerators only read data in the collection; they cannot be used to modify the underlying collection. Example: C# foreach statement Counts through a list, but the statement itself cannot modify anything. int[] counter = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in counter) { DO SOMETHING} Moves implicitly through the list 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE ACTION AREA WHERE CHANGES HAPPEN 14
![2 6 IENUMERATOR String namesnew String2 Rodney Stutte Cass forIEnumerator e names Get Enumerator 2. 6 IENUMERATOR String names[]=new String[2] {”Rodney”, ”Stutte”, ”Cass”}; for(IEnumerator e =names. Get. Enumerator();](https://slidetodoc.com/presentation_image_h2/4046ffe271e17afbb28bd28db4f1ec06/image-15.jpg)
2. 6 IENUMERATOR String names[]=new String[2] {”Rodney”, ”Stutte”, ”Cass”}; for(IEnumerator e =names. Get. Enumerator(); e. Move. Next(); Response. Write(e. Current)); CURRENT, MOVENEXT, RESET Output: Rodney Stutte Cass Moves explicitly through the list Via e. Move. Next(); 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 15

2. 7 IENUMERABLE IEnumerable is a great example of an interface. IEnumerable defines just one method: Get. Enumerator. This method returns an Enumerator object for a collection and that lets you step through the collection with the For. . . Each syntax. 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 16

3 WHAT TYPE OF COLLECTION? Sequential List Index Access Key/Value or both Sortable List Fast Searches Only String Type 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 17

SEQUENTIAL LIST • Sequential list where the element is typically discarded after its value is retrieved • Queue / Queue generic class / FIFO behavior. • Stack class / Stack generic class / LIFO behavior. • The Linked. List generic class allows sequential access either from the head to the tail or from the tail to the head. 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 18

ACCESS BY INDEX • The Array. List and String. Collection classes and the List generic class offer access to their elements by the zero-based index of the element. • The Hashtable, Sorted. List, List. Dictionary, and String. Dictionary classes, and the Dictionary and Sorted. Dictionary generic classes offer access to their elements by the key of the element. • The Name. Object. Collection. Base and Name. Value. Collection classes, and the Keyed. Collection and Sorted. List generic classes offer access to their elements by either the zerobased index or the key of the element. 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 19

ACCESS BY KEY OR VALUE OR BOTH • One value: Use any of the collections based on the IList interface or the IList generic interface. • One key and one value: Use any of the collections based on the IDictionary interface or the IDictionary generic interface. • One value with embedded key: Use the Keyed. Collection generic class. • One key and multiple values: Use the Name. Value. Collection class. 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 20

SORTABLE LIST • The Hashtable class sorts its elements by their hash codes. • The Sorted. List class and the Sorted. Dictionary and Sorted. List generic classes sort their elements by the key, based on implementations of the i. Comparer interface and the i. Comparer generic interface. • Array. List provides a Sort method that takes an IComparer implementation as a parameter. Its generic counterpart, the List generic class, provides a Sort method that takes an implementation of the i. Comparer generic interface as a parameter. 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 21

FAST SEARCHES • List. Dictionary is faster than Hashtable for small collections (10 items or fewer). • The Sorted. Dictionary generic class provides faster lookup than the Dictionary generic class. 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 22

COLLECTIONS THAT ACCEPT ONLY STRING • String. Collection (based on IList) and String. Dictionary (based on IDictionary) are in the System. Collections. Specialized namespace. You can use any of the generic collection classes in the System. Collections. Generic namespace as strongly typed string collections by specifying the String class for their generic type arguments. 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 23

4. EXAMPLES Okay one final example 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 24

ONE FINAL EXAMPLE Output True 1000 using System; using System. Collections; class Program { static Hashtable Get. Hashtable() { // Create and return new Hashtable hashtable = new Hashtable(); hashtable. Add("Area", 1000); hashtable. Add("Perimeter", 55); hashtable. Add("Mortgage", 540); return hashtable; } static void Main() { Hashtable hashtable = Get. Hashtable(); // See if the Hashtable contains this key. Console. Write. Line(hashtable. Contains. Key("Perimeter")); // Test the Contains method. It works the same way. Console. Write. Line(hashtable. Contains("Area")); // Get value of Area with indexer. int value = (int)hashtable["Area"]; // Write the value of Area. Console. Write. Line(value); } } 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 25

5. REFERENCES Kanjilal J, Working with collections in C#, retrieved on Dec 15, 2010 from http: //aspalliance. com/854 Allen Sam, C# Hashtable Use, Lookups and Examples, retrieved on Dec 15, 2010 from http: //dotnetperls. com/hashtable MSDN, Creating and Manipulating Collections, retrieved on Dec 15, 2010 from http: //msdn. microsoft. com/en-us/library/14 ek 9 axh(v=vs. 80). aspx MSDN, IEnumerable Interface, retrieved on Dec 15, 2010 from http: //msdn. microsoft. com/en-us/library/14 ek 9 axh(v=vs. 80). aspx Albahari Joseph, C# Concepts: Value vs Reference Types, retrieved on Dec 15, 2010 from http: //www. albahari. com/valuevsreftypes. aspx 12/31/2021 Lakshmi Mareddy CIS 525 Bellevue University NE 26