Created
May 30, 2018 17:43
-
-
Save baruchiro/ab24f8e0f0adb03891fb774d79eb3b3c to your computer and use it in GitHub Desktop.
Sample of extension methods for matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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