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
34
votes
2 answers

Create a delegate when there is a conditional attribute

I have a Portable Class Library with a class PCLDebug: public static class PCLDebug { public static Action LogLine { get; set; } } What I want to do is set things up once in the outer project, then be able to call LogLine within the PCL…
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
34
votes
3 answers

C# - Event keyword advantages?

I've come to recently understand that a C# 'event' really is. It isn't really anything, honestly. To sum up my findings: The event keyword is simply a modifier that only applies to delegates. So, all the 'magic' of an event are the operations of a…
Michael Yanni
  • 1,476
  • 4
  • 17
  • 29
34
votes
6 answers

Practical use of interface events

What is a good example of the power of interface events (declaring events inside interface)? Most of the times I have seen only public abstract methods inside interface.
user160677
  • 4,233
  • 12
  • 42
  • 55
34
votes
11 answers

tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

I have a weird problem. I get this error: -[FourSquareCheckInViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7aecc0 2012-09-14 19:18:39.039 [5869:707] *** Terminating app due to uncaught exception…
jimbob
  • 3,288
  • 11
  • 45
  • 70
33
votes
7 answers

Delegates Vs. Notifications in iPhoneOS

I am trying to call a method in my root view controller from a child view controller such that when I change my options they will automatically update the root view, which will in turn update several other view controllers. For the second part I…
michael f golden
  • 333
  • 1
  • 4
  • 5
33
votes
2 answers

Delegate for an Action< ref T1, T2>

I'm trying to create a delegate of a static method which takes a ref argument. Please don't ask why I'm doing such a cockamamie thing. It's all part of learning how .Net, C#, and reflection work and how to optimize it. My code is: public…
Max Yaffe
  • 1,317
  • 1
  • 14
  • 26
33
votes
4 answers

Pass and execute delegate in separate AppDomain

I want to exceute some piece of code in separate AppDomain with delegate. How can I do this? UPD1: some more details about my problem My program processing some data (one iteration is: get some data from DB, evaluate it and create assemblies at…
lak-b
  • 2,115
  • 4
  • 18
  • 30
33
votes
9 answers

Delegates in C#

I`m having some trouble in understanding how delegates in C# work. I have many code examples, but i still could not grasp it properly. Can someone explain it to me in "plain english"? Of course! examples of code will help, but i think i need more of…
George Silva
  • 3,454
  • 10
  • 39
  • 64
32
votes
5 answers

Passing a Function (with parameters) as a parameter?

I want to create a generic to which I can pass a function as a parameter, however this function may include parameters itself so... int foo = GetCachedValue("LastFoo", methodToGetFoo) Such that: protected int methodToGetFoo(DateTime today) { return…
klkitchens
  • 1,202
  • 2
  • 16
  • 39
32
votes
1 answer

Implement delegates within SwiftUI Views

I am trying to implement a functionality that requires a delegate method (like NSUserActivity). Therefore I need a UIViewController that conforms to NSUserActivityDelegate (or similar other delegates), handles and hold all the required information.…
l30c0d35
  • 777
  • 1
  • 8
  • 32
32
votes
2 answers

Active Record with Delegate and conditions

Is it possible to use delegate in your Active Record model and use conditions like :if on it? class User < ApplicationRecord delegate :company, :to => :master, :if => :has_master? belongs_to :master, :class_name => "User" def has_master? …
Fousa
  • 535
  • 1
  • 7
  • 10
32
votes
8 answers

Is there a way to create a delegate to get and set values for a FieldInfo?

For properties there are GetGetMethod and GetSetMethod so that I can do: Getter = (Func)Delegate.CreateDelegate(typeof(Func), propertyInfo.GetGetMethod()); and Setter = (Action
nawfal
  • 70,104
  • 56
  • 326
  • 368
32
votes
3 answers

Builds a Delegate from MethodInfo?

After googling and landing on SO and having read this other question Is it possible to build a correct Delegate from a MethodInfo if you didn't know the number or types of parameters at compile time? More on this: can this be done elegantly…
chakrit
  • 61,017
  • 25
  • 133
  • 162
32
votes
2 answers

Creating delegate from MethodInfo

I am currently running into an issue trying to create delegates from MethodInfo. My overall goal is to look through the methods in a class and create delegates for ones marked with a certain attribute. I am trying to use CreateDelegate but I am…
thecaptain0220
  • 2,098
  • 5
  • 30
  • 51
32
votes
2 answers

Extension methods defined on value types cannot be used to create delegates - Why not?

Extension methods can be assigned to delegates that match their usage on an object, like this: static class FunnyExtension { public static string Double(this string str) { return str + str; } public static int Double(this int num) { return…
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964