Explain the difference ...
Explain the difference ...
Certainly! I'd be happy to explain the difference between the Select
and Where
methods in C#.
The Select
method is used to project each element of a sequence into a new form. It is used to transform or map each element of a sequence into a new element based on a specified selector function. The Select
method returns a new sequence containing the transformed elements.
Here's an example:
var numbers = new[] { 1, 2, 3, 4, 5 };
var squares = numbers.Select(n => n * n);
In this example, the Select
method is used to transform each number in the numbers
array into its square value, resulting in a new sequence squares
containing the squared values.
The Where
method is used to filter elements from a sequence based on a specified condition. It returns a new sequence containing only the elements that satisfy the specified predicate.
Here's an example:
var numbers = new[] { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
In this example, the Where
method is used to filter the numbers
array and create a new sequence evenNumbers
containing only the even numbers.
Select
is used for projec...senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào