Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 23 / 68

Transforming an Array (LINQ Select)

In this lesson you will learn how to transform an array with LINQ's Select in C#, so you can process every element in one go. This is for anyone searching for "C# LINQ Select how to use".

LINQ's .Select(function) applies the same operation to every element of an array and produces a new one. Think of it as a machine on a conveyor belt that processes every product that passes through it in the same way.

The sample code uses nums.Select(n => n * 2).ToArray() to build a new array doubled in which every element of the original {1, 2, 3} has been doubled. The n => n * 2 form is called a lambda expression and defines the operation inline.

A common stumbling block for beginners is forgetting .ToArray(). On its own, .Select() leaves you with a lazily evaluated type, so you have to convert explicitly when you want a real array.

In real-world development, LINQ methods chain together, so you can express several transformations and filters on a single line - a technique you will meet constantly.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)