Generics Flashcards

1
Q

With generics what can you do?

A

With generics, you can create classes and methods that are
independent of contained types.

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

What is generics?

A

Generics in C# allow you to define classes, methods, delegates, and interfaces with a placeholder for the type of data they store or use.

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

What are the benefits of generics?

A

Code reusability, type safety, and performance are benefits.

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

Value types are stored on the ______, reference types are stored on the ______.

A

stack, heap.

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

What is boxing?

A

The conversion from a value type to a reference type is known as boxing.

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

What is unboxing?

A

When a boxed value type can be converted to a value type.

With unboxing, the cast operator is required.

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

Is an array size static?

A

An array’s size is set at compile-time and is static

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

How is an ArrayList different from arrays?

A
  • Items can be added and removed dynamically, and the ArrayList resizes itself.
  • ArrayList belongs to System.Collections namespace.
  • Add() method can be used to add items.
  • ArrayList is not type-safe.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are some benefits of generic lists compared to arrays and arraylists?

A
  • List<T> stores elements of the specified type and it grows automatically.</T>
  • List<T> can store multiple null and duplicate elements.</T>
  • List<T> can be accessed using indexer, for loop or foreach statement.</T>
  • LINQ can be used to query List<T> collection.</T>
  • List<T> is ideal for storing and retrieving large number of elements.</T>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the syntax to create a default constructor for an empty list?

A

List<int> myList = new List<int>();</int></int>

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

What’s the syntax to create a default constructor for a list with a capacity of 10?

A

List<int> myList = new List<int>(10);</int></int>

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

What does this code do?

A

Sets the myList capacity to 20.

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

What does this code do?

A

TrimExcess removes the unneeded capacity. However, because the relocation takes time, TrimExcess has no effect if the item count is more than 90 percent of capacity.

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

What does this code do?

A

Creates a list of int type and assigns it values and then prints “Total elements: “ and the count of all the elements.

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

Create the code for a foreach loop that iterates through the items in myList and prints the item

A

foreach (var item in myList)
Console.WriteLine(item);

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

Create the code for a foreach method that iterates through the items in myList and prints the item

A

myList.ForEach(item => Console.WriteLine(item));

17
Q

Create the code for a for loop that iterates through the items in myList and prints the item

A

for (int i=0; i<myList.Count; i++)
Console.WriteLine(myList[i]);

18
Q

What is the syntax for the insert method?

A

list_name.Insert(int index, T item);

18
Q

What is the syntax for the remove method?

A

list_name.Remove(T item);

19
Q

What is the syntax for the removeAt method?

A

list_name.RemoveAt(int index);

20
Q

What is IndexOf?

A

The method IndexOf requires an object as parameter and returns the index of the first occurrence of the item if it is found inside the collection.

If the item is not found, -1 is returned.

21
Q

What is the contains method?

A

The method Contains requires an object as parameter and returns true if the item is found inside the collection.

If the item is not found, false is returned.

22
Q

What is the find method?

A

The method Find requires a predicate match or a lambda expression as parameter and returns the object itself if it is found inside the collection.

If the object is not found, null is returned.

23
Q

What is a dictionary?

A

is a generic collection that stores key-value pairs. It provides fast lookups, additions, and removals of elements by their keys. The TKey represents the type of keys in the dictionary, and TValue represents the type of values.

24
Q

What does the ContainsKey() method do?

A

Checks if a key exists in the dictionary.

25
Q

What is the syntax for a dictionary named myDictionary that takes in int as a key and string as it’s value?

A

Dictionary<int, string> myDictionary = new Dictionary<int, string>();

26
Q

What is a queue?

A

A queue is a collection whose elements are processed first-in-first-out (FIFO).

27
Q

Can you access queue elements using an indexer?

A

False.

28
Q

What is the enqueue method?

A

A method that allows you to add an item to it, which is put at the end of the queue.

29
Q

What is the dequeue method?

A

A method to get items from the beginning of the queue.

30
Q

What is a stack?

A

The item that is added last to the stack is read first, so the stack is a last-in-first-out (LIFO) container.

31
Q

What method adds an item to the stack?

A

Push.

32
Q

What method removes the item that was added last?

A

Pop.