10. Element Operations Flashcards

1
Q

What does the First extension method do?

A

Returns the first element of a sequence.

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

What does the Last extension method do?

A

Returns the last element of a sequence.

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

What does the FirstOrDefault extension method do?

A

Returns the first element of a sequence if it’s not empty. If it is, returns the default value for the sequence’s element type.

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

What happens if you call First or Last method on an empty sequence?

A

An exception gets thrown.

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

What does the LastOrDefault extension method do?

A

Returns the last element of a sequence if it’s not empty. If it is, returns the default value for the sequence’s element type.

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

What does the ElementAt extension method do, why would you want to use it, and why would you not?

A
  1. Returns the element at a specified index in a sequence.
  2. Because Enumerable is more generic, and a collection represented by enumerable may not have an indexer.
  3. Don’t use ElementAt() where you can use an index. It’s probably not going to be as efficient.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if you pass the ElementAt extension method an index that’s greater than or equal to the length of the sequence that’s being operated on?

A

It throws an exception.

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

What does the ElementAtOrDefault extension method do?

A

Returns the element at a specified index in a sequence if it exists. If not, it returns the default value for the sequence’s element type.

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

What does the Single extension method do and when would it throw an exception?

A

It returns the only element in a sequence that returns true when passed to a specified predicate function. If more than one elements return true, it throws an exception.

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

What does the SingleOrDefault extension method do and when would it throw an exception?

A

It returns the only element in a sequence that returns true when passed to a specified predicate function. If no elements do, it returns the default value for the element type. If more than one elements do, it throws an error.

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