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

Explain the difference ...

Câu trả lời

Certainly! I'd be happy to explain the difference between the Select and Where methods in C#.

Select Method

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:

csharp Copy
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.

Where Method

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:

csharp Copy
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.

Key Differences

  1. Purpose: Select is used for projec...
senior

senior

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

senior

What is Indexer in C#?

expert

Why Abstract class can not be sealed or static?

junior

What you understand by Value types and Reference types in .NET? Provide some comparison

Bình luận

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

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