Index
- Introduction
- Arrays
- Generics
- Lists
- Dictionary
- Using sets
- Queues and stacks
- Choose your collection
- References
Introduction
This post is part of a series of post to help you prepare the MCSD certification, particularly the certification exam 70-483, based on the book:You will find all the code available in the following GitHub repository. Lets talk a little bit about threads and how they work using a .Net development environment.
Arrays
This is the most basic data structure:- its size is specified during the creation process. (restrictive)
- zero-based
- it implements IEnumerable so you can use a foreach loop on it
- Dimensions:
- single dimension
- multidimensional
- jagged arrays
Generics
Most of the collection structures have both generic (C# 2.0 - System.Collections.Generic namespace) and non-generic (System.Collections namespace) versions. The generic collection is used when you work with objects of one specific type. This improves type safety and performance.
By using a value type as the parameter for a generic collection, you need to make sure that there is no any scenario where boxing could occur. If the value type does not implement IEquatable<T>, you object needs boxing to call Object.Equals(Object) to check equality.
Lists
This structure is similar to arrays but with some enhancements:
- You can add additional items.
- It implements IList and ICollection which allows you to call the following methods:
- IndexOf()
- Insert(int index, T item)
- RemoveAt(int index)
- Count()
- Add(T item)
- Clear()
- Contains(T item)
- CopyTo(T[] array, int arrayIndex)
- Remove(T item)
- Duplicates
- Fast
Dictionary
It stores items and retrieve them by key:
- Dictionary<TKey,TValue>
- Don't duplicate keys
- Two types, one for key and one for value
- Fast O(1)
- The hash value of a key shouldn't change and can't be null.
- The value can be null (reference type)
Using sets
In Java exists a type called "set". In C# that's a reserved word but you can use the class HashSet<T>. This is a particular collection with no duplicates and no particular order. You can add new object to the structure and iterate over them.
Queues and stacks
Queue features:
- FIFO (first in, first out)
- By getting an item you remove it from the Queue
- Typically used for messaging systems
- Methods:
- Enqueue(): adds and element
- Dequeue(): removes the oldest element
- Peek(): returns the oldes element but doesn't immediately remove it from the queue.
Stack features:
- LIFO (last in, first out)
- By getting an item you remove it from the stack
- Methods:
- Push(): adds a new item to the stack
- Pop(): gets the newest item form the stack
- Peek(): gets the newest item without removing it
Choose your collection
List and Dictionary offer random access. Dictionary offers faster read features, but it can't store duplicates.
Queue and Stack are used when you want to retrive items ina specific order.
Set-based collections don't offer random access to individual items.
List is used more commonly but it worth to work out which collection fits better with our requirements.
0 comments:
Post a Comment