C# Collections Flashcards

1
Q

What is a collection?

A

a type whose purpose is to manage a group of objects in memory and to provide access to those objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Microsoft collections types

A

Collections can be categorized as:

List - Dictionaries - Sets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Lists

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Dictionaries

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Sets

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What type of operations a collection needs to support?

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Array?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

IEnumerable

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ICollection

A

“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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

IEnumerator GetEnumerator

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

IList

A

“You can access my elements with an index”
Array implements IList
Ability to enumerate element (index, indexOf)
Ability to modify the collection (Insert(), RemoveAt())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

IDictionary

A

like a list, but with keys instead of an index

it’s a collection of key value pair

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

ISet

A

“Can do set operation with other collections”
Set operation, set comparison, Modify the collection
less widely used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

List

A

It’s a like an array. It gives a high performance and an index-based + adding and removing elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Collection

A

Provides an implementation of IList

Collection is customizable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

IObservable

A

design to be a list to provide notification if the collection has changed

17
Q

LinkedList

A

purpose: Collection that is quick at adding and removing elements

Pros: Good at adding/removing elements, enumerating,
CON: No index-based access, use lots of memory

18
Q

Stack

A

The element place you can add or remove an element is at the last place.

Stack are referred to as last-in-first-out collection.

add - push is used to add element to the stack

remove - pop is used to remove element to the stack example: a stack book

19
Q

Queue

A

you can only add/remove the first element.

Queue are referred to as first-in-first-out collection.

enqueue - to add element
dequeue - to remove element
example: schedule, task are done in order.

20
Q

Dictionary

A

A Dictionary class is a data structure that represents a collection of keys and values pair of data. The key is identical in a key-value pair and it can have at most one value in the dictionary, but a value can be associated with many different keys.

21
Q

HashSet

A

has no sense of order. An element is in the collection or isn’t. Elements do not have index. There is no way to look an element.
HashSet guarantees uniqueness: never have the same element more than once.

22
Q

IEnumerable

IEnumerator

A

IEnumerable
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

IEnumerator
it’s like a cursor. has a method call MoveNext(), and a property call current.
bool MoveNext() – return true if there is a next item.
the enumerator will stop when there is no more item.
IEnumerator implement IDisposable.