Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 38 - C# Tutorial - Delegates Example - II

Click here for the article related to Delegates Example Part I
Click here for the article related to Delegates Example Part II
Part 38 - C# Tutorial - Delegates Example - II


12 comments:

  1. fire our employees? haha

    ReplyDelete
  2. Delegates : -

    I have written the code like below but not working it has been showing errors i was unable to find out the errors please tell me that what is the wrong with below code.Thank you...

    using System;
    using System.Collections.Generic;

    public class WithDelClassEx {

    public static void Main() {

    List emplist=new List();

    emplist.Add(new Employee() {Id=1, Name="sivakumar", Experience=8});
    emplist.Add(new Employee() {Id=2, Name="Aravind", Experience=6});
    emplist.Add(new Employee() {Id=3, Name="jagadeesh", Experience=10});
    emplist.Add(new Employee() {Id=4, Name="subbalakshmi", Experience=9});

    IsPromotable isPromotable=new IsPromotable(EligibleEmp);

    public static bool EligibleEmp(Employee ee) {
    if(ee.Experience>=8) {
    return true;
    }
    else {
    return false;
    }
    }

    Employee.PromoteEmployee(emplist, isPromotable);

    }
    }

    delegate bool IsPromotable(Employee emp1);

    class Employee {

    public int Id{ set; get; }
    public string Name{ set; get; }
    public int Experience{ set; get; }

    public static void PromoteEmployee(List emp, IsPromotable IsEligibleToPromote) {
    foreach(Employee employee in emp) {
    if(IsEligibleToPromote(employee)) {
    Console.WriteLine("Employee"+" "+employee.Name+" "+"Promoted");
    }
    }
    }

    }

    ReplyDelete
    Replies
    1. because a function can never be in Main function..kindly from func from Main method

      Delete
  3. Same Code As demonstrated in video.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
    class Delegate
    {
    public static void Main()
    {
    List emplist = new List();
    emplist.Add(new Employee() { ID = 101, Name = "Mohan", Salary = 5000, Experience = 5 });
    emplist.Add(new Employee() { ID = 102, Name = "Alan", Salary = 6000, Experience = 6 });
    emplist.Add(new Employee() { ID = 103, Name = "Suresh", Salary = 7000, Experience = 3 });
    emplist.Add(new Employee() { ID = 104, Name = "Jay", Salary = 4000, Experience = 3 });
    emplist.Add(new Employee() { ID = 105, Name = "Sachin", Salary = 4500, Experience = 3 });
    IsPromotable del = new IsPromotable(Promote);
    Employee.PromoteEmployee(emplist,del);
    }
    public static bool Promote(Employee emp)
    {
    if(emp.Experience >=5)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }
    delegate bool IsPromotable(Employee empl);
    class Employee
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public int Salary { get; set; }
    public int Experience { get; set; }

    public static void PromoteEmployee(List employeelist, IsPromotable ispromotable)
    {
    foreach (Employee employee in employeelist)
    {
    if (ispromotable(employee))
    {
    Console.WriteLine(employee.Name + " is promoted");
    }
    }
    }
    }
    }

    ReplyDelete
  4. Modified Code:
    __________________________



    using System;
    using System.Collections.Generic;

    public class WithDelClassEx
    {

    public static void Main()
    {

    List emplist = new List();

    emplist.Add(new Employee() { Id = 1, Name = "sivakumar", Experience = 8 });
    emplist.Add(new Employee() { Id = 2, Name = "Aravind", Experience = 6 });
    emplist.Add(new Employee() { Id = 3, Name = "jagadeesh", Experience = 10 });
    emplist.Add(new Employee() { Id = 4, Name = "subbalakshmi", Experience = 9 });

    IsPromotable isPromotable = new IsPromotable(EligibleEmp);
    Employee.PromoteEmployee(emplist, isPromotable);
    }

    static bool EligibleEmp(Employee ee)
    {
    if (ee.Experience >= 8)
    {
    return true;
    }
    else
    {
    return false;
    }
    }




    }

    delegate bool IsPromotable(Employee emp1);

    class Employee
    {

    public int Id { set; get; }
    public string Name { set; get; }
    public int Experience { set; get; }

    public static void PromoteEmployee(List emp, IsPromotable IsEligibleToPromote)
    {
    foreach (Employee employee in emp)
    {
    if (IsEligibleToPromote(employee))
    {
    Console.WriteLine("Employee" + " " + employee.Name + " " + "Promoted");
    }
    }
    }

    }

    _________________________

    1.using System.Collections.Generic.List requires 1 typ argument.List emplist=new List();

    2. Delegate function IsPromotable() need to be declare outside the main method

    3. corrected syntax error and scope of access modifier

    ReplyDelete
    Replies
    1. using System;
      using System.Collections.Generic;

      public class WithDelClassEx
      {

      public static void Main()
      {

      List emplist = new List();

      emplist.Add(new Employee() { Id = 1, Name = "sivakumar", Experience = 8 });
      emplist.Add(new Employee() { Id = 2, Name = "Aravind", Experience = 6 });
      emplist.Add(new Employee() { Id = 3, Name = "jagadeesh", Experience = 10 });
      emplist.Add(new Employee() { Id = 4, Name = "subbalakshmi", Experience = 9 });

      IsPromotable isPromotable = new IsPromotable(EligibleEmp);
      Employee.PromoteEmployee(emplist, isPromotable);
      }

      static bool EligibleEmp(Employee ee)
      {
      if (ee.Experience >= 8)
      {
      return true;
      }
      else
      {
      return false;
      }
      }




      }

      delegate bool IsPromotable(Employee emp1);

      class Employee
      {

      public int Id { set; get; }
      public string Name { set; get; }
      public int Experience { set; get; }

      public static void PromoteEmployee(List emp, IsPromotable IsEligibleToPromote)
      {
      foreach (Employee employee in emp)
      {
      if (IsEligibleToPromote(employee))
      {
      Console.WriteLine("Employee" + " " + employee.Name + " " + "Promoted");
      }
      }
      }

      }

      Delete
  5. hiii sir you pass expression to delegates use emp=>emp.experiace>=5 but not mention about emp .so what it is

    ReplyDelete
  6. Your tutorials are awesome. You can clearly see that you put in much effort in creating them and that you know what you are talking about. Thank you so much!

    ReplyDelete
  7. the second parameter is type delegate of employee that is why just by using lamda we get experience

    ReplyDelete
  8. Hello sir, you are doing a fabulous job. Thanks for this awesome videos series. Appreciate that.

    ReplyDelete
  9. This is really amazing. I tried many videos for delegates and even from udemy but did not get the clarity. And then I saw this video. Wow

    ReplyDelete
  10. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApp3
    {
    class Delegate
    {
    public static void Main()
    {
    List emplist = new List();
    emplist.Add(new Employee() { ID = 101, Name = "Mohan", Salary = 5000, Experience = 5 });
    emplist.Add(new Employee() { ID = 102, Name = "Alan", Salary = 6000, Experience = 6 });
    emplist.Add(new Employee() { ID = 103, Name = "Suresh", Salary = 7000, Experience = 3 });
    emplist.Add(new Employee() { ID = 104, Name = "Jay", Salary = 4000, Experience = 3 });
    emplist.Add(new Employee() { ID = 105, Name = "Sachin", Salary = 4500, Experience = 3 });
    IsPromotable del = new IsPromotable(Promote);
    Employee.PromoteEmployee(emplist, del);
    Console.ReadLine();
    }
    public static bool Promote(Employee emp)
    {
    if (emp.Experience >= 5)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }
    delegate bool IsPromotable(Employee empl);
    class Employee
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public int Salary { get; set; }
    public int Experience { get; set; }

    public static void PromoteEmployee(List employeelist, IsPromotable ispromotable)
    {
    foreach (Employee employee in employeelist)
    {
    if (ispromotable(employee))
    {
    Console.WriteLine(employee.Name + " is promoted");
    }
    }
    }
    }
    }

    ReplyDelete

It would be great if you can help share these free resources