C# Collections Flashcards
What is a collection?
a type whose purpose is to manage a group of objects in memory and to provide access to those objects.
Microsoft collections types
Collections can be categorized as:
List - Dictionaries - Sets
Lists
Order is important is a list. When need to look up position in a list.
index based
Array T[] and List are both index based list.
other index based list:
Collection
ReadOnlyCollection
ObservableCollection
Index based list are good for memory use and they are also efficient for accessing elements.
Dictionaries
Not interested in index, but access items using its name. When you want to access some element in the collections you identified it using some piece of information that is unique to that element.
Dictionary
Dictionary are meant to be search using a key.
Sets
The focus is on treating the collection as a single group and performing operation on the collection as a whole.
The set can allow you to find the intersect (common) in two sets or the union of two sets.
the most common set collection is: HashSet
What type of operations a collection needs to support?
Two Types:
- Reading
Look up element
enumerating - ask for all items in collection
collections that do not allow you to look up items: Sets, Linked Lists, Stacks, Queues - Writing Add an element Remove an element Lists: Insert an element (replace an element)
Array?
The most basic type of collection in .NET
an array is an index-based list.
array offer an very rich API, very lightweight and offer best performance for looking up elements, special c# syntax, they are fixed sized.
array is index based, first element is at index 0 and final element is at index (# of element) - 1.
arrays are reference typed. each element store their address which refer to the object
IEnumerable
the most important interface. It purpose is that it declare that you can iterate my element. If something implements IEnumerable, then you can use LINK
Enumerable don’t necessary know how many items there are. IT’s read onlyon it.
ICollection
“I know how many elements I have”
Identifies something as a collection.
Collection are expected to know how many items they contains.
It tells you that it’s a collection, but it doesn’t tell you what kind of collection it is.
IList, IDIctionary, ISet implements ICollection
IEnumerator GetEnumerator
Enumerating a collection is done by a separate object named enumerator. The sole purpose of IEnumerator is to supply the enumerator.
Returns an enumerator
- the thing that does the enumerating
IList
“You can access my elements with an index”
Array implements IList
Ability to enumerate element (index, indexOf)
Ability to modify the collection (Insert(), RemoveAt())
IDictionary
like a list, but with keys instead of an index
it’s a collection of key value pair
ISet
“Can do set operation with other collections”
Set operation, set comparison, Modify the collection
less widely used
List
It’s a like an array. It gives a high performance and an index-based + adding and removing elements.
Collection
Provides an implementation of IList
Collection is customizable