Questions tagged [delegates]

Delegates can refer to several concepts. An object can rely on another (a delegate) to perform a function. Delegation can also refer to programming language feature making use of the method lookup rules for dispatching self-calls. In C#, a delegate defines which method to call when an event is triggered.

In object-oriented programming, there are three related notions of delegation.

Most commonly, delegation refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls. Delegation as a language feature supports the prototype-based programming model.

Delegation can also refer to one object relying upon another to provide a specified set of functionalities. In research, this is often referred to as consultation or as aggregation in modeling.

In C#, a delegate is a way of telling which method to call when an event is triggered, keeping the method type.

A Delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure.

Official Source

11320 questions
83
votes
4 answers

Performance of calling delegates vs methods

Following this question - Pass Method as Parameter using C# and some of my personal experience I'd like to know a little more about the performance of calling a delegate vs just calling a method in C#. Although delegates are extremely convenient, I…
Paolo
  • 22,188
  • 6
  • 42
  • 49
79
votes
4 answers

C#: Difference between ' += anEvent' and ' += new EventHandler(anEvent)'

Take the below code: private void anEvent(object sender, EventArgs e) { //some code } What is the difference between the following ? [object].[event] += anEvent; //and [object].[event] += new EventHandler(anEvent); [UPDATE] Apparently,…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
79
votes
8 answers

Creating delegates manually vs using Action/Func delegates

Today I was thinking about declaring this: private delegate double ChangeListAction(string param1, int number); but why not use this: private Func ChangeListAction; or if ChangeListAction would have no return value I could…
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
79
votes
6 answers

Simple Delegate (delegate) vs. Multicast delegates

I have gone through many articles but I am still not clear about the difference between the normal delegates that we usually create and multicast delegates. public delegate void MyMethodHandler(object sender); MyMethodHandler handler = new…
A9S6
  • 6,575
  • 10
  • 50
  • 82
79
votes
5 answers

Using delegates in C#

In C# language and .NET framework, could you help me with understanding delegates? I was trying to check some code, and found that the results I received were unexpected for me. Here it is: class Program { public static int I = 0; static…
user1859587
  • 771
  • 5
  • 9
78
votes
4 answers

How to change uitableview delete button text

Hi there I am trying to change the text that is showing in the delete button when a user swipes a uitableviewcell inside my tableview. I have seen an example in another question thread that says to use this tableview delegate - (NSString…
C.Johns
  • 10,185
  • 20
  • 102
  • 156
76
votes
2 answers

Func delegate with ref variable

public object MethodName(ref float y) { // elided } How do I define a Func delegate for this method?
chugh97
  • 9,602
  • 25
  • 89
  • 136
75
votes
6 answers

iOS: Using UIView's 'drawRect:' vs. its layer's delegate 'drawLayer:inContext:'

I have a class which is a subclass of UIView. I am able to draw stuff inside the view either by implementing the drawRect method, or by implementing drawLayer:inContext: which is a delegate method of CALayer. I have two questions: How to decide…
Itamar Katz
  • 9,544
  • 5
  • 42
  • 74
74
votes
8 answers

Create empty C# event handlers automatically

It is not possible to fire an event in C# that has no handlers attached to it. So before each call it is necessary to check if the event is null. if ( MyEvent != null ) { MyEvent( param1, param2 ); } I would like to keep my code as clean as…
Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92
74
votes
5 answers

Declare a delegate type in Typescript

Coming from a C# background, I want to create a datatype that defines a function signature. In C#, this is a delegate declared like this: delegate void Greeter (string message); public class Foo { public void SayHi (Greeter g) { …
Dynalon
  • 6,577
  • 10
  • 54
  • 84
73
votes
9 answers

How do you use Func<> and Action<> when designing applications?

All the examples I can find about Func<> and Action<> are simple as in the one below where you see how they technically work but I would like to see them used in examples where they solve problems that previously could not be solved or could be…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
72
votes
7 answers

Events - naming convention and style

I'm learning about Events / Delegates in C#. Could I ask your opinion on the naming/coding style I've chosen (taken from the Head First C# book)? Am teaching a friend about this tomorrow, and am trying to come up with the most elegant way of…
Dave Mateer
  • 6,588
  • 15
  • 76
  • 125
72
votes
4 answers

In .NET, what thread will Events be handled in?

I have attempted to implement a producer/consumer pattern in c#. I have a consumer thread that monitors a shared queue, and a producer thread that places items onto the shared queue. The producer thread is subscribed to receive data...that is, it…
Ben
  • 729
  • 1
  • 5
  • 3
71
votes
2 answers

5 years later, is there something better than the "Fastest Possible C++ Delegates"?

I know that the topic of "C++ delegates" has been done to death, and both http://www.codeproject.com and http://stackoverflow.com deeply cover the question. Generally, it seems that Don Clugston's fastest possible delegate is the first choice for…
Dinaiz
  • 2,213
  • 4
  • 22
  • 32
70
votes
2 answers

How to correctly unregister an event handler

In a code review, I stumbled over this (simplified) code fragment to unregister an event handler: Fire -= new MyDelegate(OnFire); I thought that this does not unregister the event handler because it creates a new delegate which had never been…
gyrolf
  • 3,772
  • 5
  • 26
  • 22