Musings
Public · Protected · Private
Func Delegates in LINQ
-
2012-07-19 00:25//Following are some Func Delegates that are built-in //public delegate TR Func<TR>(); //public delegate TR Func<T0, TR>(T0 a0); //public delegate TR Func<T0, T1, TR>(T0 a0, T1 a1); //public delegate TR Func<T0, T1, T2, TR>(T0 a0, T1 a1, T2 a2); //public delegate TR Func<T0, T1, T2, T3, TR>(T0 a0, T1 a1, T2 a2, T3 a3); // simply define a int[] int[] ints = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Declare our delegates on top of defined delegates. Func<int, int> MultiplyWithThree = i => i * 3; Func<int, bool> GreaterThanTwo = i => i > 2; Func<int, int, int> MultiplyTwo = (i,j) => i * j; Func<double, double, double> MultiplyTwoDoubles = (i, j) => i * j; Func<double, double, double, double> MultiplythreeDoubles = (i, j, k) => i * j * k; Func<double, double, double, double, double> MultiplyFourDoubles = (i, j, k, L) => i * j * k * L; //Use them IEnumerable<int> intsMultipliedByThree = ints.Select(MultiplyWithThree); IEnumerable<int> intsGreaterThanTwo = ints.Where(GreaterThanTwo); IEnumerable<int> intsMultiplied = from i in ints from j in ints where i ==j select (MultiplyTwo(i, j)); IEnumerable<double> doublessMultiplied = from i in ints from j in ints where i == j select (MultiplyTwoDoubles((double)i, (double)j)); IEnumerable<double> intsThreeMultiplied = from i in ints from j in ints from k in ints where i == j && j == k select (MultiplythreeDoubles(i, j, k)); IEnumerable<double> intsFourMultiplied = from i in ints from j in ints from k in ints from l in ints where i == j && j == k select (MultiplyFourDoubles(i, j, k,l));
This blog is frozen. No new comments or edits allowed.