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
38
votes
5 answers

Does jQuery have a handleout for .delegate('hover')?

I am trying to use: $('mydiv').delegate('hover', function() { $('seconddiv').show(); }, function() { //For some reason jQuery won't run this line of code $('seconddiv').hide(); });
Chris Abrams
  • 39,732
  • 19
  • 51
  • 57
38
votes
7 answers

How do I Unregister 'anonymous' event handler

Say if I listen for an event: Subject.NewEvent += delegate(object sender, NewEventArgs e) { //some code }); Now how do I un-register this event? Or just allow the memory to leak?
P.K
  • 18,587
  • 11
  • 45
  • 51
37
votes
9 answers

Automatically delegating all methods of a java class

Say I have a class with many of public methods: public class MyClass { public void method1() {} public void method2() {} (...) public void methodN() {} } Now I would like to create a wrapper class which would delegate all the…
walkeros
  • 4,736
  • 4
  • 35
  • 47
37
votes
5 answers

Several UIAlertViews for a delegate

Currently I've got a class popping up UIAlertViews here and there. Currently, the same class is the delegate for these (it's very logical that it would be). Unfortunately, these UIAlertViews will call the same delegate methods of the class. Now, the…
quano
  • 18,812
  • 25
  • 97
  • 108
37
votes
4 answers

Co- and Contravariance bugs in .NET 4.0

Some strange behavior with the C# 4.0 co- and contravariance support: using System; class Program { static void Foo(object x) { } static void Main() { Action action = _ => { }; // C# 3.5 supports static co- and contravariant…
controlflow
  • 6,667
  • 1
  • 31
  • 55
36
votes
4 answers

Does assigning null remove all event handlers from an object?

I have defined new member in my class protected COMObject.Call call_ = null; This class has the following event handler that I subscribed to call_.Destructed += new COMObject.DestructedEventHandler(CallDestructedEvent); Will setting my member to…
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
36
votes
1 answer

Recursive call - Action lambda

What am I doing wrong here? How can I execute my action? var recurse = new Action((item, depth) => { if (item.Items.Count() > 0) recurse(item, depth + 1); // red squiggly here // ... }); I'm getting a red squiggly when…
cllpse
  • 21,396
  • 37
  • 131
  • 170
35
votes
2 answers

Inline delegate declaration (c#)

I can't get the following to compile: var x = new Action(delegate void(){}); Can anyone point out what I'm doing wrong?
maxp
  • 24,209
  • 39
  • 123
  • 201
35
votes
7 answers

In C#, why can't I test if a event handler is null anywhere outside of the class that it's defined?

I am sure that I am just not understanding something fundamental about events and/or delegates in C#, but why can't I do the Boolean tests in this code sample: public class UseSomeEventBase { public delegate void SomeEventHandler(object sender,…
gabe
  • 1,873
  • 2
  • 20
  • 36
34
votes
6 answers

anonymous delegates in C#

I can't be the only one getting tired of defining and naming a delegate for just a single call to something that requires a delegate. For example, I wanted to call .Refresh() in a form from possibly other threads, so I wrote this code: private void…
Nefzen
  • 7,819
  • 14
  • 36
  • 34
34
votes
3 answers

Parameter Action in which T3 can be optional

I have the following code: public static MyMethod() { ...Do something ProtectedMethod(param1, param2); ...Do something } protected static void ProtectedMethod(IEnumerable param1, string param2, int param3 = 1) { …
Sergio Romero
  • 6,477
  • 11
  • 41
  • 71
34
votes
6 answers

Why can a .NET delegate not be declared static?

When I try to compile the following: public static delegate void MoveDelegate (Actor sender, MoveDirection args); I receive, as an error: "The modifer 'static' is not valid for the this item." I'm implementing this within a singleton, with a…
zeboidlund
  • 9,731
  • 31
  • 118
  • 180
34
votes
4 answers

Why do 2 delegate instances return the same hashcode?

Take the following: var x = new Action(() => { Console.Write("") ; }); var y = new Action(() => { }); var a = x.GetHashCode(); var b = y.GetHashCode(); Console.WriteLine(a == b); Console.WriteLine(x == y); This will…
leppie
  • 115,091
  • 17
  • 196
  • 297
34
votes
9 answers

C# Dynamic Event Subscription

How would you dynamically subscribe to a C# event so that given a Object instance and a String name containing the name of the event, you subscribe to that event and do something (write to the console for example) when that event has been fired? It…
DAC
  • 1,769
  • 1
  • 20
  • 32
34
votes
4 answers

Why Are Some Closures 'Friendlier' Than Others?

Let me apologize in advance - I'm probably butchering the terminology. I have a vague understanding of what a closure is, but can't explain the behaviour I'm seeing. At least, I think it's a closure issue. I've searched online, but haven't found…
Rob P.
  • 14,921
  • 14
  • 73
  • 109