This is called projection which is called Select
in LINQ. That does not return a new array (like how JavaScript’s .map
does), but an IEnumerable<T>
. You can convert it to an array with .ToArray
.
using System.Linq; // Make 'Select' extension available
...
var ages = people.Select(person => person.Age).ToArray();
Select
works with all IEnumerable<T>
which arrays implement. You just need .NET 3.5 and a using System.Linq;
statement.
For your 2nd example use something like this. Notice there are no arrays in use – only sequences.
var items = Enumerable.Range(1, 4).Select(num => string.Format("{0}a", num));