Initialize2DArray
Initializes a 2D array of the given width, height and value.
- Use
Enumerable.Repeat()
to repeatvalue
width
times, convert to an array and repeatheight
times using the same method. - Use
IEnumerable.Select()
andIEnumerable.First()
to convert the jagged array to a 2D array.
using System.Linq;
public static partial class _30s
{
public static T[,] Initialize2DArray<T>(int width, int height, T value)
{
return new [] { new T [height, width] }
.Select(_ => new { x = _, y = Enumerable.Repeat(
Enumerable.Repeat(value, width).ToArray(), height
)
.ToArray()
.Select((a, ia) => a.Select((b, ib) => _[ia, ib] = b).Count()).Count() }
)
.Select(_ => _.x)
.First();
}
}