Câu hỏi phỏng vấn C#
Câu hỏi

What is the yield ke...

Câu trả lời

The yield keyword in C# is used to create an iterator method that allows you to iterate over a collection one item at a time, rather than returning the entire collection at once[1][2][3][4]. It is particularly useful when working with large collections of data, as it allows you to process the data incrementally without having to load the entire collection into memory[2].

Here's how the yield keyword works:

  1. You define a method that returns an IEnumerable or IEnumerator and use the yield return statement to return each item in the collection[1][3].
  2. When the method is called, the compiler generates a state machine that keeps track of the current position in the iteration[3].
  3. Each time yield return is encountered, the current state is saved, and the method returns the yielded value to the caller[1][4].
  4. On subsequent calls, the method resumes from the saved state, continuing the iteration until the next yield return or yield break statement is reached[1][4].

The yield key...

senior

senior

Gợi ý câu hỏi phỏng vấn

junior

What is the difference between a class and a structure ?

middle

What is difference between constant and readonly ?

senior

IEnumerable vs List - What to Use? How do they work?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào