Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - What is the point of declaring Delegates?

Tags:

c#

delegates

I'm trying to understand Delegates in C#. I'm not too deep down the rabbit hole yet; as far as I know they are just pointers. I made the mistake of trying to apply JavaScript logic to C# when trying to understand them originally (everything is let or const) however soon realized there was no way to assign methods. So I understand why we need Delegates, my question is:

Why do we declare them?

Instead of:

public delegate void Pointer(String text);
Pointer p = SomeMethod;

Why not just do:

var p = SomeMethod;

???

I'm sure there must be some advanced things I don't know yet but I just can't see why you would ever need to declare them the first way???

like image 760
Robert Quayle Avatar asked Oct 14 '25 16:10

Robert Quayle


1 Answers

Why do we declare them?

We declare everything in C#. Well, apart from dynamic (but even then, it's a front for a dictionary).

Why not just do var p = SomeMethod;

Try doing that on a .net framework project; you'll get an error "cannot assign method group to implicitly typed local variable". Some of the advice you find floating round on the internet predates the invention of the .net you're using and so targets older patterns of doing things

I just can't see why you would ever need to declare them the first way?

If you look at the evolution of C# over the time it's always trying to find ways of allowing one to more compactly/neatly express their intent. What started out as quite a verbose syntax for (insert operation like "declaring a property" here) comes down in characters over time, usually because the bits that people use a lot become wearisome to keep typing out (even with propfull-tab-tab) or even read

Since the introduction of generics it's been possible to use Action<...> and Func<...> instead of declaring one's own delegate types, but delegate preceded generics so for a good while we used the delegate keyword a lot.

Those years of "doing it this way" builds up an inertia of documentation/teaching that doesn't change overnight. No one sees C# 10 come out and immediately go through all their old blogs and throw away/rewrite the advice to do X some old way that the newest C# makes simpler; they instead write another blog post about the new features which usually helps people transition if they're used to the old way.

It also stands to reason that if you're learning about delegates then a web search would bring up a tutorial that mentions the word. Such a tutorial would perhaps most sensibly start off at some very base level of "how we do it" because it ties in with the rest of the concepts of the language - here's how we declare a class that represents a person, here's how we declare a delegate that represents an operation that takes an int and returns a string - for a tutorial to start out at the highest level of convenience that C# offers and say "just say var p = SomeMethod, and make sure to omit () so you don't call the method, and C# will do the rest of the trickery needed to make an object that refers to the method" doesn't teach a lot about what goes on under the hood.

As a relative newcomer without that background knowledge of how it was to relate to, you're relating it to other languages, which can be problematic (especially something fairly fast-n-loose like JavaScript) but in recent versions of C# you absolutely can do as you propose (var p = SomeMethod). If you land a job working with some N year old code base you'll probably find delegate ... in there somewhere so having the familiarity with it you've acquired will help you read it even if you cannot migrate it to a more modern incarnation..

like image 154
Caius Jard Avatar answered Oct 17 '25 06:10

Caius Jard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!