C# Generics Flashcards
1
Q
What is ildasm?
A
- ships with .net, can show intermediate language code of exes or DLLs
2
Q
Why generics?
A
- allow code reuse with type safety
- defer type specification to client
3
Q
What is a List?
A
- generic Collection
- basically an array that manages its own size
- List
- can pass in starting size in constructor
- will double in size as list grows … 0,4,8,16 … as items are added
4
Q
What is Queue?
A
- similar to list, generic collection
- no add method, use Enqueue
- use Dequeue - returns first in line and removes it from the queue
- FIFO data structure
- use Peek() to look at next item; doesn’t remove from Queue
- can search Queue with .Contains
5
Q
What is Stack?
A
- LIFO data structure
- stack.Push() - to add things to stack
- stack.Pop() - most recently added object, then removes from Stack
- stack.Peek() - get last item added but doesn’t remove from Stack
6
Q
What is a Hashset?
A
- similar to List
- order of items is not guaranteed
- set.Add() - returns true/false
- won’t accept duplicates
- you can use special operations on Hashsets
- set1.IntersectWith(set2)
- set1.UnionWith(set2)
- set1.SymmetricExceptWith(set2) - returns items in set1 and set2 but NOT in both
- Hashset by default doesn’t know how to compare complex objects; you have to tell it how to compare
7
Q
What is LinkedList?
A
- very concerned with order of items
- AddFirst, AddLast, AddBefore, AddAfter
- RemoveFirst, RemoveLast, etc
- very efficient in adjusting the list
8
Q
What is Dictionary?
A
- key and value store
- can’t add duplicate keys
- dict.ContainsKey
- there is a SortedDictionary, sorts on key
9
Q
What is the T?
A
- type parameter
- the standard is using T
- anything can be used to represent type
10
Q
What are “Sorted” collections?
A
- SortedSet - sorted & unique
- SortedList - sorted & memory efficient
- SortedDictionary - sorted, fast inserts & removals
11
Q
What does Compare typically do?
A
- comes from C language
- returns an integer
- if 0 then equal
- if less than 0 then first is less than second
- if greater than 0 then first is greater than second
- ex String.Compare(a, b)
12
Q
How can you convert types?
A
- doesn’t work on all types, mostly primitive types
- var conv = TypeDescriptor.GetConverter(typeof(T));
- convert.ConvertTo(item, typeof(TOutput))
13
Q
How do you return data when building your own Enumerator?
A
- foreach loop of objects
- yield return result
- basically a lazy return
14
Q
What is included in method signature?
A
- everything but the return type including any generic type parameters
15
Q
What are extension methods?
A
- static class and static methods
- first parameter is “this IBuffer buffer”