Click here for the article related to Delegates Example Part I
Click here for the article related to Delegates Example Part II
Click here for the article related to Delegates Example Part II
Part 38 - C# Tutorial - Delegates Example - II
Free C#, .Net and Sql server video tutorial for beginners and intermediate programmers.
fire our employees? haha
ReplyDeleteDelegates : -
ReplyDeleteI 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");
}
}
}
}
because a function can never be in Main function..kindly from func from Main method
DeleteSame Code As demonstrated in video.
ReplyDeleteusing 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");
}
}
}
}
}
Modified Code:
ReplyDelete__________________________
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
using System;
Deleteusing 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");
}
}
}
}
hiii sir you pass expression to delegates use emp=>emp.experiace>=5 but not mention about emp .so what it is
ReplyDeleteYour 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!
ReplyDeletethe second parameter is type delegate of employee that is why just by using lamda we get experience
ReplyDeleteHello sir, you are doing a fabulous job. Thanks for this awesome videos series. Appreciate that.
ReplyDeleteThis 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
ReplyDeleteusing System;
ReplyDeleteusing 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");
}
}
}
}
}