C# Collections Fundamentals

Starting from arrays and progressing to lists, dictionaries, and sets, this course covers the capabilities of the various collection types, how they work under the hood, and performance implications.
Course info
Rating
(1205)
Level
Intermediate
Updated
Apr 8, 2014
Duration
5h 56m
Table of contents
Course Welcome
Introducing C# Collections
Inside Arrays
The Array Type
Collection Interfaces
Lists
Linked Lists, Stacks and Queues
Dictionaries
Sets
Enumerators
Multidimensional Arrays, Rank and Bounds
Description
Course info
Rating
(1205)
Level
Intermediate
Updated
Apr 8, 2014
Duration
5h 56m
Description

This course aims to empower you to take advantage of the surprisingly rich set of collections that are available in C#. You'll learn how to use the various collection types to store data as arrays, lists, dictionaries, linked lists, or sets, as well as how to customize collection behavior in C#. You'll also learn how many of these types work under the hood and the implications for performance. This course also covers enumerators and the collection interfaces.

About the author
About the author

Simon is a senior developer with over 20 years programming experience, particularly covering C++ and the .NET Framework. He believes knowledge is to be shared and has written or co-written more than a dozen books for professional programmers.

More from the author
C# Concurrent Collections
Advanced
3h 32m
15 May 2015
C# Equality and Comparisons
Intermediate
4h 51m
10 Sep 2014
Math For Programmers
Intermediate
5h 16m
26 Aug 2013
Section Introduction Transcripts
Section Introduction Transcripts

Introducing C# Collections
Hello. This is a C# collections fundamentals course. I'm Simon Robinson, and this is the module Introduction to Collections. In this module I'll give you a high-level overview of collections in C#. You'll gain an understanding of the main types of collections that exist in programming in general, things like lists and dictionaries, and once you have that background I'll hone in much more on C# and. NET and point out in very broad high-level terms what. NET offers you as a C# programmer in terms of collections. In more detail what I'm going to cover is I'll examine what a collection is and the kinds of operations a collection is responsible for. I'll explain the purposes of the main categories of collections including lists, dictionaries, and sets and how you would use each one. A lot of this knowledge applies in any language or to any collections library by the way, not just C#. I'll go over a quick history of how the. NET collections have evolved from their. NET 1. 0 origins and I'll show you how this leads to the various collections and namespaces that are available today. I'm not really going to go into detail on any particular classes yet. That will come in later modules. The aim here is rather to give you a high-level view of the collection landscape so that when we do later on look at individual classes you will better understand how each class fits into the overall picture. Also, I'll mention there's basically no coding in this module since it's fairly conceptual in nature, but if you want code don't worry. There will be lots of coding later on once we have the concepts sorted. At the end of this module, you should have a good understanding of the overall kinds of collections available in C# and which namespaces to look for them in.

Inside Arrays
This is the C# collections course. I'm Simon Robinson, and this is the first of two modules in which we're going to talk about arrays, the most basic collection type in. NET. Having a good understanding of arrays is crucially important in order to give a solid basis for understanding more advanced. NET collection types, so I'm going to spend quite a bit of time on arrays. This module will focus on arrays themselves, what arrays are, how they work, and the syntax for basic operations in C#. The next module will look more at the implementation of arrays in. NET including how arrays fit into the. NET type system and the rich functionality that the managed array type implements. So, in this module I'll review what an array is and the basic syntax for using it. I'll show you how arrays work under the hood. If you understand this, you'll understand why arrays are so efficient for accessing data and also the reasons for the limitations on what they can do. Declaring and initializing arrays. C# offers a variety of syntaxes for declaring arrays and initializing their contents, and I'll cover how you can do that. Enumerating through arrays. There are two ways of enumerating or iterating through the contents of arrays using a foreach loop or using a for loop. I'll show you both techniques and compare the advantages of each one.

The Array Type
Hi. I'm Simon Robinson. This is the C# collections course, and this module is about The Array Type. You've seen how arrays work and the syntax for basic array operations. This module will focus on the features of arrays as a. NET type. There are broadly three areas to cover. I'll start by looking at how arrays fit into the. NET type system, and we will see some of the consequences of arrays being reference types. I'll look at the fact that you can store derived type instances in an array and show you how this relates to array covariance. And finally, I'll go over some of the nice capabilities that arrays offer by their various methods and properties, and I'll make sure you're aware of the main things you can do with them beyond basic collection functionality, stuff like copying arrays, sorting their contents, or finding elements in them.

Lists
Hello. This is the Pluralsight C# collections course. My name is Simon Robinson, and in this module we're going to talk about index-based lists. In fact, this is the first of four modules in which we will get down and explore the main types of collection that Microsoft offers for general purpose use, index-based lists, more specialized collections such as linked lists, dictionaries, and the sets. I should also give a disclaimer that there are too many Microsoft collection types for me to cover every single one of them, but I will make sure you're up to speed on all the important core generic collections, the ones you are likely to need in normal everyday coding scenarios. So, the particular classes that we will learn about in this module are List<T>, which provides a very powerful index-based list with an extensive API similar to array, but with the additional feature of being able to add and remove elements; ReadOnlyCollection<T>, which provides a read-only wrapper around other lists; Collection<T>, which gives you the ability to customize the behavior of a list; and ObservableCollection<T>. This class provides a list that allows for notifications whenever the list is modified. Of these types, List<T> is in the System. Collections. Generic namespace while the others are in System. Collections. ObjectModel. Once you're familiar with these types along with arrays, you will be well set up to use index-based lists to their full potential in your code.

Linked Lists, Stacks and Queues
Hi. I'm Simon Robinson. This is the Pluralsight C# collections course, and this module is about Linked Lists, Stacks, and Queues. It's the second of four modules covering the main collection types, and the common theme that runs through this module is that the types we'll be looking at are all by nature lists, and they're all very useful types, but they don't provide index-based element lookup because they're designed for scenarios that don't require that. And given the title of this module, the list of classes that we're going to learn about will not come as any surprise to you. LinkedList<T>. This is a list that allows very fast adding and removing elements, and it's used in scenarios where you need to modify the list very often. We'll also learn about LinkedListNode<T>, a special class that facilitates putting elements in a linked list. Stack<T> allows you to build first-in-last-out lists. And finally, Queue<T> provides a collection in which the items are removed in the same order that they are added, that is first-in-first-out. And these classes are all in the System. Collections. Generic namespace.

Dictionaries
Hello. This is a C# collections course. I'm Simon Robinson, and this module is about Dictionaries. We will cover Dictionary <TKey, TValue>, which represents a good general purpose dictionary and is very widely used; ReadOnlyDictionary<TKey, TValue>, which as the name suggests provides read-only access to a dictionary; SortedList<TKey, TValue> and SortedDictionary<TKey, TValue>, these both provide dictionaries that automatically sort their elements; and finally KeyedCollection<TKey, TValue>, which is customizable and adds some useful features to dictionaries. Along the way you'll also learn a little of how hash tables work and how the hash codes in. NET are related to dictionaries.

Sets
Hello. This is a C# collections course. I'm Simon Robinson, and this module is about Sets. In fact, this is the final of the four modules in which we've been looking at the different. NET collection types and what they are used for. We've covered index-based Lists; Linked Lists, Stacks, and Queues; and Dictionaries, and Sets are the one remaining collections category to be examined. So, in this module you'll learn about the HashSet<T> and SortedSet<T> types. Both of these types implement the ISet<T> interface to perform set operations and comparisons, and that means you'll implicitly learn about ISet<T> as well. And you'll also learn how both of these sets can be very useful for providing a collection that guarantees uniqueness. This is a very common reason for using sets, even when you don't need to perform set operations.

Enumerators
Hi. I'm Simon Robinson. This is the Pluralsight C# collections course, and this module is about the. NET enumeration model, in other words how enumerators work. At this stage of the course you have seen quite a number of. NET collection classes, and you've seen how for basically every collection C# lets you enumerate the elements using a foreach loop. The various collection classes are massively different from each other in how they work, yet somehow C# manages to keep exactly the same foreach syntax for everyone of them. What I've not yet explained is how the compiler manages to achieve that magic using enumerators. This module will rectify that. You will learn all about enumeration in. NET including how the IEnumerable<T> and IEnumerator<T> interfaces work together under the hood to iterate elements of a collection, how the C# foreach statement uses enumerators shielding you from all the complexity of enumerating, how the collection classes protect you from collections changing as you enumerate them, how to write your own enumerators, and finally I will talk a little bit about how covariance works for enumerators. By the end of this module, you should have a thorough understanding of how enumerators work for. NET collections. Let's begin.

Multidimensional Arrays, Rank and Bounds
Hello. I'm Simon Robinson, and this is the final module of the C# Collections Fundamentals course. At this point you've almost completed the course, and there's just one final area we need to look at. That's the options that arrays give you for having different values and numbers of indices including multidimensional arrays. We'll be covering just a few topics. Multidimensional Arrays. These are a special kind of array that you can use for dealing with situations where your data is grid-like in nature. Rank and Bounds. We'll look at the members that array makes available to retrieve its dimensions. This will lead us into the area of indices that are not zero-based. Jagged Arrays. This is an alternative to multidimensional arrays, which gives a bit more flexibility at the cost of more complicated syntax. Jagged arrays can also be generalized to other collection types. Both multidimensional and jagged arrays are techniques that you can use when your data is more complex than a simple list. I'll show you how to use both techniques and compare the advantages of each one.