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.
                                	
	delegates
    
                            
                        
                    
            11,363
            questions
        
        
            1152
            votes
        
        
            15
            answers
        
        
            638k
            views
        
    Can I call a constructor from another constructor (do constructor chaining) in C++?
                As a C# developer I'm used to running through constructors:
class Test {
    public Test() {
        DoSomething();
    }
    public Test(int count) : this() {
        DoSomethingWithCount(count);
  ...
            
        
       
    
            1144
            votes
        
        
            12
            answers
        
        
            305k
            views
        
    Why would you use Expression<Func<T>> rather than Func<T>?
                I understand lambdas and the Func and Action delegates. But expressions 
stump me. 
In what circumstances would you use an Expression<Func<T>> rather than a plain old Func<T>?
            
        
       
    
            913
            votes
        
        
            13
            answers
        
        
            986k
            views
        
    Pass Method as Parameter using C#
                I have several methods all with the same parameter types and return values but different names and blocks. I want to pass the name of the method to run to another method that will invoke the passed ...
            
        
       
    
            880
            votes
        
        
            3
            answers
        
        
            449k
            views
        
    Func vs. Action vs. Predicate [duplicate]
                With real examples and their use, can someone please help me understand:
When do we need a Func<T, ..> delegate?
When do we need an Action<T> delegate?
When do we need a Predicate<T>...
            
        
       
    
            774
            votes
        
        
            20
            answers
        
        
            385k
            views
        
    How do I create delegates in Objective-C?
                I know how delegates work, and I know how I can use them.
But how do I create them?
            
        
       
    
            706
            votes
        
        
            7
            answers
        
        
            296k
            views
        
    Func delegate with no return type
                All of the Func<T> delegates return a value. What are the .NET delegates that can be used with methods that return void?
            
        
       
    
            602
            votes
        
        
            8
            answers
        
        
            138k
            views
        
    How can I make a weak protocol reference in 'pure' Swift (without @objc)
                weak references don't seem to work in Swift unless a protocol is declared as @objc, which I don't want in a pure Swift app.
This code gives a compile error (weak cannot be applied to non-class type ...
            
        
       
    
            430
            votes
        
        
            8
            answers
        
        
            620k
            views
        
    When & why to use delegates? [duplicate]
                I'm relatively new in C#, & I'm wondering when to use Delegates appropriately.
they are widely used in events declaration, but when should I use them in my own code and why are they useful? why ...
            
        
       
    
            376
            votes
        
        
            11
            answers
        
        
            156k
            views
        
    What are the differences between delegates and events?
                What are the differences between delegates and an events? Don't both hold references to functions that can be executed?
            
        
       
    
            256
            votes
        
        
            14
            answers
        
        
            90k
            views
        
    Unsubscribe anonymous method in C#
                Is it possible to unsubscribe an anonymous method from an event?
If I subscribe to an event like this:
void MyMethod()
{
    Console.WriteLine("I did it!");
}
MyEvent += MyMethod;
I can un-...
            
        
       
    
            224
            votes
        
        
            14
            answers
        
        
            302k
            views
        
    Java Delegates?
                Does the Java language have delegate features, similar to how C# has support for delegates?
            
        
       
    
            194
            votes
        
        
            6
            answers
        
        
            75k
            views
        
    delegate keyword vs. lambda notation
                Once it is compiled, is there a difference between:
delegate { x = 0; }
and
() => { x = 0 }
?
            
        
       
    
            178
            votes
        
        
            6
            answers
        
        
            204k
            views
        
    What is a C++ delegate?
                What is the general idea of a delegate in C++? What are they, how are they used and what are they used for?
I'd like to first learn about them in a 'black box' way, but a bit of information on the ...
            
        
       
    
            176
            votes
        
        
            4
            answers
        
        
            47k
            views
        
    Why are Objective-C delegates usually given the property assign instead of retain?
                I'm surfing through the wonderful blog maintained by Scott Stevenson, and I'm trying to understand a fundamental Objective-C concept of assigning delegates the 'assign' property vs 'retain'. Note, the ...
            
        
       
    
            166
            votes
        
        
            13
            answers
        
        
            76k
            views
        
    What is Delegate? [closed]
                I am confused that what is the actual role of a delegate? 
I have been asked this question many times in my interviews, but I don't think that interviewers were satisfied with my answer.
Can anyone ...
            
        
       
    
            165
            votes
        
        
            10
            answers
        
        
            40k
            views
        
    Delegates: Predicate vs. Action vs. Func
                Can someone provide a good explanation (hopefully with examples) of these 3 most important delegates:
Predicate
Action
Func
            
        
       
    
            165
            votes
        
        
            4
            answers
        
        
            39k
            views
        
    What's the best way to communicate between view controllers?
                Being new to objective-c, cocoa, and iPhone dev in general, I have a strong desire to get the most out of the language and the frameworks.
One of the resources I'm using is Stanford's CS193P class ...
            
        
       
    
            164
            votes
        
        
            12
            answers
        
        
            188k
            views
        
    Function Pointers in Java
                This may be something common and trivial, but I seem to be having trouble finding a concrete answer. In C# there is a concept of delegates, which relates strongly to the idea of function pointers from ...
            
        
       
    
            163
            votes
        
        
            1
            answer
        
        
            60k
            views
        
    Difference Between Invoke and DynamicInvoke
                What is the difference between Invoke and DynamicInvoke in delegates? Please give me some code example which explain difference between that two methods.
            
        
       
    
            156
            votes
        
        
            12
            answers
        
        
            147k
            views
        
    How to hide the keyboard when I press return key in a UITextField?
                Clicking in a textfield makes the keyboard appear.
How do I hide it when the user presses the return key?
            
        
       
    
            153
            votes
        
        
            9
            answers
        
        
            49k
            views
        
    Why can't an anonymous method be assigned to var?
                I have the following code:
Func<string, bool> comparer = delegate(string value) {
    return value != "0";
};
However, the following does not compile:
var comparer = delegate(string value) {
 ...
            
        
       
    
            152
            votes
        
        
            10
            answers
        
        
            129k
            views
        
    How can I clear event subscriptions in C#?
                Take the following C# class:
c1 {
 event EventHandler someEvent;
}
If there are a lot of subscriptions to c1's someEvent event and I want to clear them all, what is the best way to achieve this? ...
            
        
       
    
            145
            votes
        
        
            15
            answers
        
        
            189k
            views
        
    Delegates in swift?
                How does one go about making a delegate, i.e. NSUserNotificationCenterDelegate in swift?
            
        
       
    
            144
            votes
        
        
            9
            answers
        
        
            169k
            views
        
    What is Func, how and when is it used
                What is Func<> and what is it used for?
            
        
       
    
            143
            votes
        
        
            8
            answers
        
        
            162k
            views
        
    Super-simple example of C# observer/observable with delegates
                I recently started digging into C# but I can't by my life figure out how delegates work when implementing the observer/observable pattern in the language.
Could someone give me a super-simple example ...
            
        
       
    
            139
            votes
        
        
            4
            answers
        
        
            135k
            views
        
    How do I set up a simple delegate to communicate between two view controllers?
                I have two UITableViewControllers and need to pass the value from the child view controller to the parent using a delegate. I know what delegates are and just wanted to see a simple to follow example.
...
            
        
       
    
            134
            votes
        
        
            9
            answers
        
        
            123k
            views
        
    Uses of Action delegate in C# [closed]
                I was working with the Action Delegates in C# in the hope of learning more about them and thinking where they might be useful.
Has anybody used the Action Delegate, and if so why? or could you give ...
            
        
       
    
            133
            votes
        
        
            9
            answers
        
        
            71k
            views
        
    Why must a lambda expression be cast when supplied as a plain Delegate parameter
                Take the method System.Windows.Forms.Control.Invoke(Delegate method)
Why does this give a compile time error:
string str = "woop";
Invoke(() => this.Text = str);
// Error: Cannot convert lambda ...
            
        
       
    
            131
            votes
        
        
            15
            answers
        
        
            50k
            views
        
    Wrap a delegate in an IEqualityComparer
                Several Linq.Enumerable functions take an IEqualityComparer<T>. Is there a convenient wrapper class that adapts a delegate(T,T)=>bool to implement IEqualityComparer<T>? It's easy enough ...
            
        
       
    
            127
            votes
        
        
            3
            answers
        
        
            34k
            views
        
    "Delegate subtraction has unpredictable result" in ReSharper/C#?
                When using myDelegate -= eventHandler ReSharper (version 6) issues:
  Delegate subtraction has unpredictable result
The rational behind this is explained by JetBrains here. The explanation makes ...
            
        
       
    
            126
            votes
        
        
            7
            answers
        
        
            36k
            views
        
    Are C# events synchronous?
                There are two parts to this question:
Does raising an event block the thread, or does it start execution of EventHandlers asynchronously and the thread goes continues on at the same time?
Are the ...
            
        
       
    
            114
            votes
        
        
            9
            answers
        
        
            107k
            views
        
    C# pattern to prevent an event handler hooked twice [duplicate]
                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, ...
            
        
       
    
            114
            votes
        
        
            22
            answers
        
        
            97k
            views
        
    How do I know that the UICollectionView has been loaded completely?
                I have to do some operation whenever UICollectionView has been loaded completely, i.e. at that time all the UICollectionView's datasource / layout methods should be called. How do I know that?? Is ...
            
        
       
    
            114
            votes
        
        
            6
            answers
        
        
            116k
            views
        
    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
{
    // ...
            
        
       
    
            113
            votes
        
        
            4
            answers
        
        
            37k
            views
        
    What is the difference between Func<string,string> and delegate?
                I see delegates in two forms:
A. Func<string, string> convertMethod = lambda 
B. public delegate string convertMethod(string value);
I'm uncertain of what actually the difference between ...
            
        
       
    
            112
            votes
        
        
            8
            answers
        
        
            50k
            views
        
    Where do I use delegates? [closed]
                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.
            
        
       
    
            111
            votes
        
        
            10
            answers
        
        
            80k
            views
        
    Difference between events and delegates and its respective applications [closed]
                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 ...
            
        
       
    
            108
            votes
        
        
            9
            answers
        
        
            327k
            views
        
    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 ...
            
        
       
    
            104
            votes
        
        
            20
            answers
        
        
            43k
            views
        
    When would you use delegates in C#? [closed]
                What are your usage of delegates in C#?
            
        
       
    
            104
            votes
        
        
            4
            answers
        
        
            29k
            views
        
    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 ...
            
        
       
    
            98
            votes
        
        
            10
            answers
        
        
            20k
            views
        
    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....
            
        
       
    
            95
            votes
        
        
            8
            answers
        
        
            45k
            views
        
    Proper naming convention for a .NET Delegate type? [closed]
                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 ...
            
        
       
    
            90
            votes
        
        
            1
            answer
        
        
            21k
            views
        
    What's the difference between 'weak' and 'assign' in delegate property declaration
                Whats the difference between this:
@property (nonatomic, weak) id  <SubClassDelegate> delegate; 
and this:
@property (nonatomic, assign) id  <SubClassDelegate> delegate; 
I want to use ...
            
        
       
    
            89
            votes
        
        
            7
            answers
        
        
            98k
            views
        
    Assigning to 'id<Delegate>' 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:
...
            
        
       
    
            87
            votes
        
        
            4
            answers
        
        
            87k
            views
        
    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 ...
            
        
       
    
            85
            votes
        
        
            4
            answers
        
        
            68k
            views
        
    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#....
            
        
       
    
            85
            votes
        
        
            9
            answers
        
        
            13k
            views
        
    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 ...
            
        
       
    
            85
            votes
        
        
            8
            answers
        
        
            31k
            views
        
    C# Generics won't allow Delegate Type Constraints
                Is it possible to define a class in C# such that
class GenericCollection<T> : SomeBaseCollection<T> where T : Delegate
I couldn't for the life of me accomplish this last night in .NET 3....
            
        
       
    
            81
            votes
        
        
            8
            answers
        
        
            19k
            views
        
    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<string, int, double> ChangeListAction;
or ...
            
        
       
    
            79
            votes
        
        
            7
            answers
        
        
            100k
            views
        
    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 ...