Friday, October 30, 2015

When and how to use Delegate?

A delegate is a reference to a method. Whereas objects can easily be sent as parameters into methods, constructor or whatever, methods are a bit more tricky. But every once in a while you might feel the need to send a method as a parameter to another method, and that's when you'll need delegates.

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

 namespace DelegateApp {  
  /// <summary>  
  /// A class to define a person  
  /// </summary>  
  public class Person {  
   public string Name { get; set; }  
   public int Age { get; set; }  
  }  

  class Program {  
    //Our delegate  
    public delegate bool FilterDelegate(Person p);  
    static void Main(string[] args) {  
    //Create 4 Person objects  
    Person p1 = new Person() { Name = "John", Age = 41 };  
    Person p2 = new Person() { Name = "Jane", Age = 69 };  
    Person p3 = new Person() { Name = "Jake", Age = 12 };  
    Person p4 = new Person() { Name = "Jessie", Age = 25 };  
    //Create a list of Person objects and fill it  
    List<Person> people = new List<Person>() { p1, p2, p3, p4 };  
    DisplayPeople("Children:", people, IsChild);  
    DisplayPeople("Adults:", people, IsAdult);  
    DisplayPeople("Seniors:", people, IsSenior);  
    Console.Read();  
   }  
   /// <summary>  
   /// A method to filter out the people you need  
   /// </summary>  
   /// <param name="people">A list of people</param>  
   /// <param name="filter">A filter</param>  
   /// <returns>A filtered list</returns>  
   static void DisplayPeople(string title, List<Person> people, FilterDelegate filter) {  
     Console.WriteLine(title);  
     foreach (Person p in people) {  
     if (filter(p)) {  
       Console.WriteLine("{0}, {1} years old", p.Name, p.Age);  
     }  
    }  
    Console.Write("\n\n");  
   }  
   //==========FILTERS===================  
   static bool IsChild(Person p) {  
    return p.Age <= 18;  
   }  
   static bool IsAdult(Person p) {  
    return p.Age >= 18;  
   }  
   static bool IsSenior(Person p) {  
    return p.Age >= 65;  
   }  
  }  
 }  

No comments:

Post a Comment

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code