Skip to content

Instantly share code, notes, and snippets.

@baruchiro
Created May 30, 2018 17:43
Show Gist options
  • Save baruchiro/ab24f8e0f0adb03891fb774d79eb3b3c to your computer and use it in GitHub Desktop.
Save baruchiro/ab24f8e0f0adb03891fb774d79eb3b3c to your computer and use it in GitHub Desktop.
Sample of extension methods for matrix
using System;
using System.Linq;
namespace extension
{
class Program
{
static void Main(string[] args)
{
double[][] matrix =
{
new double[] {5, 5, 5, 5, 5},
new double[] {5, 5, 5, 5, 5},
new double[] {5, 5, 5, 5, 5},
new double[] {5, 5, 5, 5, 5},
new double[] {5, 5, 5, 5, 5}
};
matrix.Print();
matrix.ForEach(x => x + 5);
matrix.Print();
Console.ReadLine();
}
}
internal static class Extensions
{
public static void ForEach(this double[][] matrix, Func<double, double> func)
{
for (var i = 0; i < matrix.Length; i++)
{
matrix[i]= matrix[i].Select(func).ToArray();
}
}
public static void Print(this double[][] matrix)
{
foreach (var d in matrix)
{
Console.WriteLine(string.Join("\t", d));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment