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
1 answer

Is there a way to store a function and it's parameters in a variable to call later?

In C#, I want to do something like this. var fn = // set it to the function and parameters it'll use fn(); In a hypothetical example, say I wanted to return a function with all of the parameters it needed to execute, I just wanted to execute it…
Yatrix
  • 13,361
  • 16
  • 48
  • 78
4
votes
1 answer

Run delegate method with BeginInvoke

In my class I have a static method public static void DoWork(int param) ... I want to run that method like: Form.BeginInvoke(DoWork, param); Is this operation possible? I tried with the MethodInvoker class ... but I don't want to define the method…
Radu D
  • 3,505
  • 9
  • 42
  • 69
4
votes
2 answers

Should I use List or simply Action to keep track of an IObservable's subscribers?

I'm implementing the IObservable interface on some classes. I used Reflector to figure out how this is typically done in Rx. Concerning how an observable keeps track of its subscribers and notifies them via their OnNext method, I stumbled upon…
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
4
votes
2 answers

SFSpeechRecognitionTaskDelegate delegate methods are not getting called

I am working on speech to text iOS 10 feature. I want SFSpeechRecognitionTaskDelegate's delegate methods to be called for checking the completed results. func speechRecognitionTask(_ task: SFSpeechRecognitionTask, didFinishRecognition…
Vin
  • 456
  • 6
  • 18
4
votes
2 answers

Can an Action be assigned to a void delegate implicitly?

I have a class that needs constructed with a void delegate: //an object that's constructed with a "void delegate of no params" public class BindableCommand { public delegate void ExecuteMethod(); private readonly ExecuteMethod…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
4
votes
3 answers

Is BeginInvoke/EndInvoke good practice for invoking on main thread?

Is it good practice to invoke delegate for MainForm thread - this way?: Txt.MainForm.EndInvoke( Txt.MainForm.BeginInvoke( new MethodInvoker(delegate() { // code here } )));
Przemysław Michalski
  • 9,627
  • 7
  • 31
  • 37
4
votes
3 answers

Delegate as function

Below function working ok but I want to make it simple. if (list.Exists(delegate(string s) { return s.Contains(str); })) { string name = list.Find(delegate(string s) { return s.Contains(str); }); } I am using delegate(string s) { return…
juniorCSharp
  • 97
  • 1
  • 6
4
votes
2 answers

Delegate function from protocol not being called

I have a previously working delegate and protocol that since the conversion to Swift 3 is no longer being called. protocol TaskCellDelegate { func doneHit(_ cell : TaskCell) } class TaskCell : UITableViewCell { var delegate :…
Michael Williams
  • 1,402
  • 2
  • 14
  • 27
4
votes
1 answer

Why does Delegate.CreateDelegate allow for invalid conversions?

Edit: I filed a bug report on microsoft connect:: https://connect.microsoft.com/VisualStudio/feedback/details/614234/delegate-createdelegate-allows-binding-functions-with-enum-parameters-to-the-enum-base-type#details Consider the following…
Michael B
  • 7,512
  • 3
  • 31
  • 57
4
votes
0 answers

UITextView Delegate is not firing shouldChangeTextInrange delegate method

I have a UITextView connected from Storyboard, call it txtView. In my ViewController I set the delegate like txtView.delegate = self. The view controller fires method like textViewDidBeginEditing and textViewDidChange, but not func textView(_…
7ball
  • 2,183
  • 4
  • 26
  • 61
4
votes
1 answer

F# Winforms Dispatcher BeginInvoke Delegate issue at runtime

I have tried to create an F# implementation of some C# code that uses Dispatcher.BeginInvoke to manipulate the UI from a different thread. However I'm struggling to get the code to work. I've tried a few different implementations but I always seem…
MattP
  • 71
  • 3
4
votes
4 answers

How to create a fully parameterized method delegate in C#?

In c# we can create delegates via a variety of means (e.g. Action<>, Func<>, delegate, lambdas, etc). But when you invoke those methods, you have to provide the parameter values for the delegate you are invoking: delegate int del(int i); del…
Adam Flynn
  • 949
  • 2
  • 9
  • 21
4
votes
2 answers

Delegates and variables scope

I have the following code: public void SetMove(Position3D pos, float time, float linearity, bool relative) { ExecuteOnActiveClients(delegate(NeuroClient client) { …
Emidee
  • 1,245
  • 1
  • 14
  • 33
4
votes
2 answers

Custom Google Sign In throw exception on GIDSignInDelegate protocol

I'm writing an iOS app in obj-c and using Google SignIn SDK to do the Google SignIn flow. I wanna be able to customize the button and action a little it so I went ahead implementing the protocols of GIDSignInDelegate myself based on their…
Ian Zhao
  • 1,145
  • 1
  • 17
  • 27
4
votes
1 answer

QML - Customizing PageIndicator to show an image for each page it indicates

UPDATE: Added code for SwipeView I'm trying to customize the PageIndicator to do what I currently have three buttons do namely Make it possible to change pages by clicking on the respective item inside the PageIndicator (currently I have 3 pages,…
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
1 2 3
99
100