Note that there are some explanatory texts on larger screens.

plurals
  1. POFailed to Understand the Use of Delegates in Real World Scenarios
    text
    copied!<p>I can't understand the proper use of delegates in .NET world. What can't be solved with Delegates? I want to know what scenario/situation is ideal or candidate for delegate usage. </p> <p>I know LINQ and Lambda expressions all use Delegates behind the scenes. I know how to create and consume it but what I have failed to understand is WHY I should create and use them?</p> <p>Any real world example or experience sharing would be highly appreciated.</p> <p>Update:</p> <p>I have created following classes </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; namespace Basics { public class Program { static void Main() { Flight.FlightTakeOffDelegate flightDelegate; var flights = new List&lt;Flight&gt; { new Flight {Number = "FL001", DepartureTime = DateTime.Now, TotalCrew = 15}, new Flight {Number = "FL002", DepartureTime = DateTime.Now.AddHours(1), TotalCrew = 15}, new Flight {Number = "FL003", DepartureTime = DateTime.Now.AddHours(2), TotalCrew = 15}, }; var tower = new FlightTower(flights); // Flight 002 asking to Take off var flightAskingToTakeOff = flights.FirstOrDefault(x =&gt; x.Number == "FL002"); if (flightAskingToTakeOff != null) { Console.WriteLine(string.Format("Flight {0} \t: Okay to take off?", flightAskingToTakeOff.Number)); flightDelegate = tower.CanTakeOff; flightAskingToTakeOff.CanTakeOff = flightDelegate.Invoke(flightAskingToTakeOff); Console.WriteLine(string.Format("Flight Tower \t: {0}", flightAskingToTakeOff.CanTakeOff)); } // Flight 001 asking to Take off flightAskingToTakeOff = flights.FirstOrDefault(x =&gt; x.Number == "FL001"); if (flightAskingToTakeOff != null) { Console.WriteLine(string.Format("Flight {0} \t: Okay to take off?", flightAskingToTakeOff.Number)); flightDelegate = tower.CanTakeOff; flightAskingToTakeOff.CanTakeOff = flightDelegate.Invoke(flightAskingToTakeOff); Console.WriteLine(string.Format("Flight Tower \t: {0}", flightAskingToTakeOff.CanTakeOff)); } Console.ReadKey(); } } public class FlightTower { private readonly List&lt;Flight&gt; _schedule; public FlightTower(List&lt;Flight&gt; schedule) { _schedule = schedule; } public bool CanTakeOff(Flight flight) { var arrivingFlights = _schedule.Where(x =&gt; x.ArrivalTime == DateTime.Now); if (!arrivingFlights.Any()) { var flightInQueue = _schedule.FirstOrDefault(x =&gt; x.DepartureTime == _schedule.Min(c=&gt; c.DepartureTime)); if (flightInQueue != null &amp;&amp; flightInQueue.Number == flight.Number) { return true; } } return false; } } public class Flight { public delegate bool FlightTakeOffDelegate(Flight flight); public string Number { get; set; } public DateTime DepartureTime { get; set; } public DateTime ArrivalTime { get; set; } public int TotalCrew { get; set; } public bool CanTakeOff { get; set; } } </code></pre> <p>}</p> <p>Can anyone please have a look to see how can delegates be used in such scenario??</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload