static void Main(string[] args) { Example ev = new Example(); ev.triggerEvent(); //multicast delegates example multicast del = new multicast(employee1); multicast del1 = new multicast(employee3); del1(); //multicast using single delegate for mutiple functions del += employee2; del(); List <Employee> empList = new List <Employee>(); Employee x = new Employee() { ID = 100, name = "tharun", Leaves = 20, Experience = 5 }; Employee y = new Employee() { ID = 101, name = "harry potter", Leaves = 5, Experience = 6 }; empList.Add(x); empList.Add(y); IsEligible EligibleForVacation = new IsEligible(Allot); Employee.Vacation(empList, Allot); }
static void DelegateExample() { // Simple example usign Delegate keyword IsEligible isEligible = delegate(Student s) { return(s.Age > 18 && s.Height > 5.5); }; Console.WriteLine(isEligible(new Student(10, 5.4f))); Console.WriteLine(isEligible(new Student(19, 5.6f))); }
//reusable method based on the criteria for vacation(i.e vacation given based on leaves, experience etc) public static void Vacation(List <Employee> employee, IsEligible e) { foreach (var emp in employee) { if (e(emp)) { Console.WriteLine(emp.name + " eligible for vacation"); } } }
static void LambdaExample() { //This is same as DelegateExample, but using Lambda expression, which means you don't have to use the delgate keyword // In s=> s.Age>18, s is the input parameter, => is the lambda operator and the rest is the // Body Lambda Expression IsEligible isEligible = s => { Console.WriteLine("calling LambdaExample()"); return(s.Age > 18 && s.Height > 5.5); }; Console.WriteLine(isEligible(new Student(10, 5.4f))); Console.WriteLine(isEligible(new Student(19, 5.6f))); }