Skip to content

Instantly share code, notes, and snippets.

@dnasca
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save dnasca/c78be1d33a17b4a6d18d to your computer and use it in GitHub Desktop.

Select an option

Save dnasca/c78be1d33a17b4a6d18d to your computer and use it in GitHub Desktop.
the goal for this Employee class is to have no hard coded logic within the class
using System;
using System.Collections.Generic;
//the goal for this Employee class is to have no hard coded logic within the class. We can accomplish this using a delegate and a single LAMBDA expression on Line 42
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int CurrentSalary { get; set; }
public int YearsExperience { get; set; }
//create 2 parameter method
public static void PromoteEmployee(List<Employee> employeeList, IsPromotable isEligibleToPromote) //delegate passed into function as a parameter
{
foreach (Employee employee in employeeList)
{
if (isEligibleToPromote(employee)) //use the logic that the delegate points to (Promote function)
{
Console.WriteLine("{0} is eligible for a promotion", employee.Name);
}
}
}
}
public delegate bool IsPromotable(Employee empl);
public class Program
{
public static void Main()
{
//instantiate a list of object of type Employee, call it empList
List<Employee> empList = new List<Employee>();
//add some objects to the list
empList.Add(new Employee() { ID = 12376, Name = "Carl Brutananadilewski", CurrentSalary = 7000, YearsExperience = 15 });
empList.Add(new Employee() { ID = 12376, Name = "Master Shake", CurrentSalary = 28000, YearsExperience = 3 });
empList.Add(new Employee() { ID = 12376, Name = "Meatwad", CurrentSalary = 5000, YearsExperience = 11 });
empList.Add(new Employee() { ID = 12376, Name = "Frylock", CurrentSalary = 302000, YearsExperience = 1 });
//invoke the PromoteEmployee method
Employee.PromoteEmployee(empList, emp => emp.YearsExperience >= 5); //method works on an Employee object and returns bool
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment