3

Iam not sure whether anyone ask the same question. But i couldnt find out any sources which are relevant for my doubt.

what is the need of delegates in real time programming? there are lot of steps needs to be done to implement it.

public delegate void SimpleDelegate();
  class TestDelegate
    {
        public static void MyFunc()
        {
            Console.WriteLine("I was called by delegate ...");
        }
        public static void Main()
        {
            // Instantiation
            SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

            // Invocation
            simpleDelegate();
        }
    }

If re usability is a main reason (since no need to create instance) for using delegate, this can be achieved by other ways also. or this is the only advantages of using delegates? I know anonymous method do a better way than delegate. But is it providing anything than delegates other than simplification of steps. and how i can reuse anonymous method when the structure will be something like given below.

List<int> values = new List<int>() { 1, 1, 1, 2, 3 };
List<int> res = values.FindAll(delegate(int element)
    {
        if (element > 10)
        {
        throw new ArgumentException("element");
        }
        if (element == 8)
        {
        throw new ArgumentException("element");
        }
        return element > 1;
    });

I am confused. Please shed some lights on this.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Tom Cruise
  • 1,395
  • 11
  • 30
  • 58
  • 1
    The difference between delegates and anonymous methods is the same as between fruits and apples. – Diligent Key Presser Mar 15 '16 at 01:30
  • Ok. But i would like to know the need rather than difference. – Tom Cruise Mar 15 '16 at 01:31
  • Well there are different types of delegates and lambda expression is one of the key core to making delegates more powerful implementation. I suggest you check out lambda to. – Aizen Mar 15 '16 at 01:32
  • http://www.lynda.com/C-tutorials/C-Delegates-Events-Lambdas/370499-2.html You can start here if you want. – Aizen Mar 15 '16 at 01:32
  • 1
    It's much like **function pointer** in C. Try solve the [24 game](https://en.wikipedia.org/wiki/24_Game) and you'll probably end up using delegates. – Lei Yang Mar 15 '16 at 01:53

2 Answers2

2

Delegates provide a fully typed and safe reference to a method. Now with generic event handlers, Action, Func and lambdas, I personally don't remember the last time I explicitly created a delegate.

They're still used all over the place under the hood, but they're pretty much history as far as day-to-day programming. Delegates existed in version 1. Lambdas and other options came along much later.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
0

With the use of Delegates, you can chain together any number of methods together, such that they can be called on when a single event is triggered. Delegates can be used to define callback methods. In Object-Oriented Development Projects, They can be very important where you want to call multiple methods at the same time. Many Senior Developers use these in different Applications of State Design patterns to call multiple methods when shifting to different concrete States.

  • Hi, welcome to the site. You seem to have described the general concepts of delegates, but the asker is asking about the specific feature in C# and whether it overlaps with other similar fetures. – markonius May 12 '22 at 11:16