using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lamda_expresion_test
{
class Program
{
public delegate int ChangeInt(int x);
public delegate int SummingInt(int x, int y);
static void Main(string[] args)
{
int[] numbers = new[] { 3, 8, 4, 6, 1, 7, 9, 2, 4, 8 };
int[] negetiveNumbers = new[] { -1,-2,-3,-4,-5,-6};
int count = numbers.Count(x => x % 2 == 0);//x % 2==1 for odd
Console.WriteLine("Total even numbers: {0}\n",count);
Console.Write("Numbers greater than 6: ");
foreach (int a in numbers.Where(p => p > 6))
Console.Write(" {0}",a);
Console.WriteLine();
//// using delegate...reducing code
ChangeInt doubleInt = delegate(int x) { return x * 2; };
Console.WriteLine(doubleInt(12));
//using Lamda Expression code more reduced
ChangeInt delgate = x => x * 2;
Console.WriteLine(delgate(12));
//Lamda expression on two parameters....
SummingInt summing = (x, y) => x + y;
Console.WriteLine(summing(12,100));
// More complex operations......
Console.WriteLine("Numbers between 3 and 8: ");
foreach (int i in numbers.Where(x => x>=3 && x<=8))
Console.Write(" {0}",i);
Console.WriteLine();
//same as previous expression......
// we can make some codition like following....
foreach (int i in numbers.Where(
x =>
{
if (x >= 3 && x <= 8)
return true;
else return false;
} ))
Console.Write(" {0}", i);
Console.WriteLine();
}
}
}
No comments:
Post a Comment