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

C# Reflection Delegate exception: Must derive from Delegate

I'm trying to understand delegations so I just wrote small try project; I have class D: class D { private static void Func1(Object o) { if (!(o is string)) return; string s = o as string; Console.WriteLine("Func1…
Erez Konforti
  • 243
  • 5
  • 19
4
votes
5 answers

Why are Action and Predicate used or defined as delegates?

Can somebody explain what is the exact reason behind using Action and Predicate as delegates in C#
Wondering
  • 4,950
  • 22
  • 71
  • 90
4
votes
3 answers

GADBannerView delegate methods not called if the view is not in the view hierarchy

I'm working with the Google Mobile Ads SDK on iOS and trying to display some ads. My code: GADBannerView* bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeFromCGSize(CGSizeMake(300, 250))]; bannerView.adUnitID =…
lawicko
  • 7,246
  • 3
  • 37
  • 49
4
votes
1 answer

F# use constructors as functions

I have a use case for treating a constructor for a derived type as a delegate and I can't figure out if it's impossible or I'm just incapable of working it out. type SomeJobEvent(jobId : int, otherThing : string) = member this.JobId = jobId …
tigerswithguitars
  • 2,497
  • 1
  • 31
  • 53
4
votes
2 answers

Abstracting gesture recognizer to two functions in Swift?

Imagine an iOS screen where you can move your finger up/down to do something (imagine say, "scale"), Or, as a separate function you can move your finger left/right to do something (imagine say, "change color" or "rotate"). They…
Fattie
  • 27,874
  • 70
  • 431
  • 719
4
votes
1 answer

Convert error between Expression and Delegate

We can write below code: Func func = x => x + x; We also can write: Expression> exp = x => x + x; But when I write : Expression> exp = func; The compiler throw an error: Cannot…
roast_soul
  • 3,554
  • 7
  • 36
  • 73
4
votes
5 answers

Method using Func as parameters

I need some help on simplifying my method I have this method public double ComputeBasicAmount(double basicLimit, double eligibleAmt) { return basicLimit * eligibleAmt; } sample usage: Foo foo = new Foo(100, 1000); double basicAmt =…
CSharpNoob
  • 1,271
  • 7
  • 18
  • 27
4
votes
1 answer

Kotlin - How to make a property delegate by map with a custom name?

I'm trying to get my head around property delegates, and I have an interesting use case. Is it possible to have something like this: class MyClass { val properties = mutableMapOf() val fontSize: Any by MapDelegate(properties,…
Ruckus T-Boom
  • 4,566
  • 1
  • 28
  • 42
4
votes
0 answers

Implicit delegate conversion not always working for delegates in another project?

Possible Duplicate: Compilation fails if delegate definitions is put in another project? Using .NET 3.5 SP1 and Visual Studio 2008 Projects A and B, both class libraries, A uses B In project B i have the following: public delegate void…
Bubblewrap
  • 7,266
  • 1
  • 35
  • 33
4
votes
1 answer

Unity/C# : How to execute a function after another function has finished its execution?

I've been trying to build a "event system" for a project that I'm working on . Here is how I'm doing it : I populate a list with reference to a gameObject and the functions that I need to execute from that gameObject . Then , when the "event" is…
Francisco M.
  • 99
  • 1
  • 2
  • 9
4
votes
3 answers

Function as method call parameter

I have a simple question that might be easily answerd but an intense use of google didn't bring up an answer to my question. So I appologize if there's the right solution and I didn't see it. If I have a method call like Object.Add(string text,…
AllDayPiano
  • 414
  • 1
  • 4
  • 20
4
votes
3 answers

QTableView, setting a cell's font and background colour

I am using QTableView and QStandardItemModel and I'm trying to colour a row with the font remaining black. I am using my delegate class's paint method: void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex…
ethane
  • 2,329
  • 3
  • 22
  • 33
4
votes
1 answer

implementing delegate in interface

I can't find a way to implement delegate in interface I want to get this: public class SomeClass : ISomeInterface { public delegate void SomeCallback(); public SomeCallback callback; public void SomeMethod() { …
4
votes
2 answers

How to know how many event handlers for an event?

How to know how many event handlers for an event? I want a way to execute the following code: // if (control.CheckedChanged.Handlers.Length == 0) { control.CheckedChanged += (s, e) => { // code; } } Note: this code is out side the…
Homam
  • 23,263
  • 32
  • 111
  • 187
4
votes
1 answer

Cannot implicitly convert type `UnityEngine.Events.UnityAction' to `UnityEngine.Events.UnityAction'

I want to send arguments through the standard Unity listener, as per the tutorial. mbListener = new UnityAction(SomeFunction); void SomeFunction(string _message) { Debug.Log ("Some Function was called!"); } Why is this failing with the…
Philip
  • 43
  • 2
  • 7
1 2 3
99
100