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
112
votes
9 answers

C# pattern to prevent an event handler hooked twice

Duplicate of: How to ensure an event is only subscribed to once and Has an event handler already been added? I have a singleton that provides some service and my classes hook into some events on it, sometimes a class is hooking twice to the event…
Ali Shafai
  • 5,141
  • 8
  • 34
  • 50
112
votes
8 answers

Where do I use delegates?

What are some real world places that call for delegates? I'm curious what situations or patterns are present where this method is the best solution. No code required.
slipsec
  • 3,004
  • 3
  • 34
  • 46
111
votes
6 answers

How to add a delegate to an interface C#

I need to have some delegates in my class. I'd like to use the interface to "remind" me to set these delegates. How to? My class look like this: public class ClsPictures : myInterface { // Implementing the IProcess interface public event…
Asaf
  • 3,067
  • 11
  • 35
  • 54
110
votes
4 answers

What is the difference between Func and delegate?

I see delegates in two forms: A. Func convertMethod = lambda B. public delegate string convertMethod(string value); I'm uncertain of what actually the difference between these two are. Are they both delegates? I believe the first…
Dietpixel
  • 9,983
  • 11
  • 28
  • 33
110
votes
10 answers

Difference between events and delegates and its respective applications

I don't see advantages of using events over delegates, other than being syntactical sugar. Perhaps I am misunderstanding, but it seems that event is just a placeholder for delegate. Would you explain to me the differences and when to use which? …
Sasha
106
votes
9 answers

Invoke(Delegate)

Can anybody please explain this statement written on this link Invoke(Delegate): Executes the specified delegate on the thread that owns the control's underlying window handle. Can anybody explain what this means (especially the bold one) I am not…
user1903439
  • 1,951
  • 7
  • 19
  • 29
104
votes
4 answers

Compiler Ambiguous invocation error - anonymous method and method group with Func<> or Action

I have a scenario where I want to use method group syntax rather than anonymous methods (or lambda syntax) for calling a function. The function has two overloads, one that takes an Action, the other takes a Func. I can happily call the two…
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
104
votes
20 answers

When would you use delegates in C#?

What are your usage of delegates in C#?
Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
98
votes
10 answers

Wrapping StopWatch timing with a delegate or lambda?

I'm writing code like this, doing a little quick and dirty timing: var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000; i++) { b = DoStuff(s); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Surely there's a way to call this…
Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
93
votes
8 answers

Proper naming convention for a .NET Delegate type?

By convention classes are often named like nouns, methods like verbs and interfaces like adjectives. What is the common naming convention for a delegate? Or what's a good way to differentiate its name when delegates are listed among types and other…
John K
  • 28,441
  • 31
  • 139
  • 229
90
votes
1 answer

What's the difference between 'weak' and 'assign' in delegate property declaration

Whats the difference between this: @property (nonatomic, weak) id delegate; and this: @property (nonatomic, assign) id delegate; I want to use property for delegates.
Firdous
  • 4,624
  • 13
  • 41
  • 80
89
votes
7 answers

Assigning to 'id' from incompatible type 'ViewController *const_strong'

Throughout my app, I'm getting semantic issue warnings when I set ViewController.delegate = self. I have searched and found similar posts but none were able to solve my problem. ViewController.m: GameAddViewController *gameAddViewContoller =…
David L
  • 4,347
  • 3
  • 18
  • 30
87
votes
4 answers

What's the method signature for passing an async delegate?

I've recently moved back to C# from being in Objective-C land, and the async/await keywords in C# 5 look cool. But I'm still trying to get a handle on the proper syntax. I want to declare a method that takes an asynchronous delegate as a parameter,…
AndrewS
  • 8,196
  • 5
  • 39
  • 53
85
votes
8 answers

C# Generics won't allow Delegate Type Constraints

Is it possible to define a class in C# such that class GenericCollection : SomeBaseCollection where T : Delegate I couldn't for the life of me accomplish this last night in .NET 3.5. I tried using delegate, Delegate, Action and Func
Nicholas Mancuso
  • 11,599
  • 6
  • 45
  • 47
85
votes
9 answers

Is there a downside to adding an anonymous empty delegate on event declaration?

I have seen a few mentions of this idiom (including on SO): // Deliberately empty subscriber public event EventHandler AskQuestion = delegate {}; The upside is clear - it avoids the need to check for null before raising the event. However, I am…
serg10
  • 31,923
  • 16
  • 73
  • 94