Download presentation
Presentation is loading. Please wait.
1
Fundaments of Game Design
C# Collections Richard Gesick
2
Objectives The nongeneric and generic collections that are provided by the .NET Framework. To use class Array’s static methods to manipulate arrays. To use the Dictionary, Stack, Queue classes To use key value pairs To use methods provided for the collection classes such as sort and contains
3
Introduction For the vast majority of applications, there is no need to build custom data structures. Instead, you can use the prepackaged data-structure classes provided by the .NET Framework. These classes are known as collection classes—they store collections of data. Each instance of one of these classes is a collection of items. Collection classes enable programmers to store sets of items by using existing data structures, without concern for how they are implemented. Programmers can code faster and expect excellent performance, maximizing execution speed and minimizing memory consumption.
4
Intro (2) The .NET Framework provides three namespaces dedicated to collections: System.Collections contains collections that store references to objects. Most new applications should use the collections in the System.Collections.Generic namespace, which contains generic collection classes. The System.Collections.Specialized namespace contains several collections that support specific types, such as strings and bits.
5
Intro (3) The collections in these namespaces provide standardized, reusable components; you do not need to write your own collection classes. These collections are written for broad reuse and tuned for rapid execution and for efficient use of memory.
6
Arrays(2) All arrays implicitly inherit from abstract base class Array (namespace System). Class Array defines property Length, which specifies the number of elements in the array. In addition, class Array provides static methods that provide algorithms for processing arrays. Many of these methods have several overloaded versions.
7
Static Methods of Arrays
When Array method Sort returns, the array contains its original elements in ascending order. int[ ] dblValues = { 3, 54,12, 3, 45, 78, 65}; Array.Sort(dblValues); Use Array method Copy to copy elements from one array to another. The first argument is the array to copy. The second argument is the destination array. The third argument is an int representing the number of elements to copy. Array.Copy(dblValues,ddblCopy,dblValues.Length);
8
Static Methods of Arrays(2)
Array method BinarySearch performs a binary search on a sorted array. Method BinarySearch receives the sorted array in which to search and the key for which to search. int result = Array.BinarySearch(dblValues,6); The method returns the index in the array at which it finds the key, or a negative number if the key was not found. Its behavior on an unsorted array is unpredictable.
9
Other static Array methods include:
Clear sets a range of elements to 0, false or null. CreateInstance creates a new array of a specified type. IndexOf locates the first occurrence of an object in an array or portion of an array. LastIndexOf locates the last occurrence of an object in Reverse reverses the contents of an array or portion of an array.
10
Generic or Parameterized Collections
To overcome the limitations of polymorphic collections, modern languages also provide a means by which you can explicitly state the type of thing you want the collection to manage. You do this via a "parameter" to the class when you define it. List<T> is called a generic class because it can be used with any type of object. T is a placeholder for the type of the objects stored in the list.
11
List The Add method appends its argument to the end of the List.
The Insert method inserts a new element at the specified position. The first argument is an index—as with arrays, collection indices start at zero. The second argument is the value that is to be inserted at the specified index.
12
List The Count property returns the number of elements currently in the List. Lists can be indexed like arrays by placing the index in square brackets after the List variable’s name. The Remove method is used to remove the first instance of an element with a specific value. If no such element is in the List, Remove does nothing. RemoveAt removes the element at the specified index; all elements above that index are shifted down by one.
13
List The Contains method returns true if the element is found in the List, and false otherwise. Contains compares its argument to each element of the List in order, so using Contains on a large List is inefficient. The Capacity property indicates how many items the List can hold without growing. List is implemented using an array behind the scenes. When the List grows, it must create a larger internal array and copy each element to the new array. A List grows only when an element is added and there is no space for the new element. The List doubles its Capacity each time it grows.
14
Sorting with generics There is a sort method that is built into the array class and the generic classes. It uses the quick sort algorithm. If you want to sort information based on classes that you wrote, your class must provide an implementation for the IComparable interface.
15
Stacks Implement a first-in-last-out behavior Push() to add an element
Pop() to remove an element Add and remove from the same side of the internal data structure Easiest to add/remove from the front if implemented as a linked list internally Peek() returns the top element without popping it from the stack
16
Stacks Which data structure would you pick to implement a Stack? Why?
17
Queues Implement a first-in-first-out behavior
Enqueue() to add an element Dequeue() to remove an element Add and remove from the opposite sides of the internal data structure If we maintain a list-tail reference, then we can add to the end and remove from the front, each having constant - O(1) - time
18
Queues Which data structure would you pick to implement a Queue? Why?
19
Dictionary class The Dictionary class represents a data structure in C# that is used to represent a collection of key and value pairs of data. The Dictionary class is a generic class and can store any data types. It is in the System.Collections.Generic namespace. The Dictionary class constructor takes two parameters (generic type), first for the type of the key and second for the type of the value. This is also known as a key-value pair. Dictionary<string, int> sub = new Dictionary<string, int>();
20
Dictionary methods Dictionaries don’t allow duplicate keys. If the key already exists and a key of the same value is added, an exception is thrown. Add: sub.Add(“A”, 5); the key is A and the value is 5 Item property works like an indexer so you can access values by calling the key Console.WriteLine("For key = \"D\", value = {0}.", sub["D"]);
21
More Dictionary methods
The indexer can be used to change the value associated with a key. sub["D"] = 7; If a key does not exist, setting the indexer for that key adds a new key/value pair. sub["F"] = 19; To print a dictionary foreach (KeyValuePair<string, int> kvp in sub) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.