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

Error Assigning Delegate Using ? : Syntax

I've created a delegate and two matching methods. private delegate bool CharComparer(char a, char b); // Case-sensitive char comparer private static bool CharCompare(char a, char b) { return (a == b); } // Case-insensitive char…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
4
votes
1 answer

ContructorInfo.Invoke vs delegate

I've seen several questions here at SO which is about getting a delegate to create an object instead of using ConstructorInfo.Invoke. Here is an example: Using a Delegate to call a constructor. I just want to know why? If it's performance-wise, why…
jgauffin
  • 99,844
  • 45
  • 235
  • 372
4
votes
1 answer

Async Delegate - What is proper approach

Ok, so, I'm almost there and need just a small bit of advise to get over this hump! So, the reason i asked this questoin Async delegate - What is proper syntax? is because I'm trying to call a delegate function asynchronously (using await ...). So,…
Robert Green MBA
  • 1,834
  • 1
  • 22
  • 45
4
votes
2 answers

iPhone SDK: Delegate method not getting called-Troubleshooting

If a Delegate method is not getting called, then what all things needs to be checked just to ensure that delegate is referenced in the viewController?
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
4
votes
1 answer

Why can't the compiler figure out that this lambda is a Delegate?

Consider this tiny program. public class Program { public static void Main() { // the first path compiles RunAction(() => { }); // the second path does not compile RunDelegate(() => {}); } private…
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
4
votes
0 answers

How to Marshall LLVMSharp Function

So teaching myself some compiler design using ANTLR4 & LLVMSharp. Trying to add 2 doubles with a marshal function and the application crashes in debug mode only (possible stack corruption?) IR generated for the example below. There is no issue when…
Chase R Lewis
  • 2,119
  • 1
  • 22
  • 47
4
votes
2 answers

How to tell code contracts a delegate specified as argument is Pure?

Consider the following code: int SomeField; void Foo([Pure] Func getData) { Contract.Requires(getData != null); Contract.Requires(getData(this.SomeField) != null); } I get the following warning: Detected call to method…
JBSnorro
  • 6,048
  • 3
  • 41
  • 62
4
votes
3 answers

In QML, (how) can I make MapItemGroup as a MapItemView's delegate component?

Situation: I am able to use QML Map item with Model/View/delegate. I am able to work with individual items. Problem: As a next step, I would like to be able to draw multiple items. I need to put multiple QML MapItems (like MapCircle, MapRectangle,…
tegginamaniss
  • 151
  • 10
4
votes
1 answer

Alternative to storing a variable in a Swift extension

I am currently working on a large app where we require the ability to track specific events (click, swipe..) on essentially any custom class derived from UIView. We have, for example, multiple subclasses of UITableView which all need to respond to…
Josh Thieme
  • 63
  • 1
  • 5
4
votes
2 answers

select anonymous delegate with linq

I know there's a way to do this but I've been banging my head against a wall trying to figure it out. This works fine: private GenericRecord CreateGeneric(GenericRecord g, Member m) { g.Member = m; return g; } public IList
Nick Spiers
  • 2,344
  • 2
  • 19
  • 31
4
votes
1 answer

Delegate.CreateDelegate to get Func<> from MethodInfo does not work for double type?

Im trying to use create a func for double.CompareTo. Im creating it like this: var method = typeof(double).GetMethod("CompareTo", new Type[] { typeof(double) }); var func = (Func
Erik83
  • 539
  • 3
  • 5
  • 19
4
votes
2 answers

How to pass a delegate as an argument to subscribe as an event handler?

I have an external application that provides an event StkQuit. I subscribe to this event in a static class that handles all communication between my application and [the] external application. I would like to subscribe to the StkQuit event using…
wulfgarpro
  • 6,666
  • 12
  • 69
  • 110
4
votes
4 answers

Your opinion of this alternative to notifications and delegates: Signals?

SO is telling me this question is subjective and likely to be closed. It is indeed subjective, because I'm asking for the opinion of experienced Objective-C developers. Should I post this somewhere else? Please advise. Fairly new to Objective-C,…
epologee
  • 11,229
  • 11
  • 68
  • 104
4
votes
2 answers

Add Jeditable fields to a delegate in jQuery

How can i combine the following with a delegate in jQuery? I have a #commentContainer surrounding all the editable elements, and I am dynamically adding editable fields (Jeditable). the editing ability is not working for dynamically loaded items. …
raklos
  • 28,027
  • 60
  • 183
  • 301
4
votes
2 answers

Confusions about weak delegate in swift

Let us suppose we have a protocol protocol MyProtocol { fun someFunc() } class AClass { var delegate: MyProtocol? } AClass doesn't care if the delegate is a class or struct. What I want is sometimes the delegate can be a class and…
echo
  • 1,244
  • 1
  • 16
  • 40