5
In PHP, can create a new array
based on a array
already existing, using the function array_map
.
With it, I can define a callback that is responsible for the definitions of each value that will be used in creating this "new array
".
Example in PHP:
$numeros = [1, 2, 3];
$numeros_dobrados = array_map(function ($value)
{
return $value * 2;
}, $numeros);
print_r($numeros); // [2, 4, 6]
I also know that you can do this in python
.
Python example:
[x * 2 for x in range(1, 10) ]
> [2, 4, 6, 8, 10, 12, 14, 16, 18]
How could I do this with this "array" in C#?
Example in C#:
var arr = new int[] {1, 2, 3}
Just so I understand:
Enumerable
is a class, andRange
is a static method? Because there was no instance... OrEnumerable
is the namespace andRange
is the class?– Wallace Maxters
Enumerable is a static class that allows you to use a static method that returns a Ienumerable<int>. The advantage of Enumerable.Range is that by using Ienumerable it allows you to use Lazy Evaluation, which is very useful in some scenarios.
– Gabriel Katakura