Where can I use delegates?

What are the real world places that delegates require? I am curious what situations or patterns are present when this method is the best solution. No code required.

+85
oop design-patterns delegates
Aug 28 '08 at 1:58
source share
8 answers

A delegate is a named type that defines a particular kind of method. Just as a class definition defines all the members for a given type of object, which it defines, the delegate sets out the method signature for the specific method that it defines.

Based on this operator, the delegate is a pointer to a function and determines what the function looks like.

A great example for a real world delegate is Predicate . In the example from the link, you will notice that Array.Find takes an array to search, and then a predicate to process the criteria of what to find. In this case, it passes the ProductGT10 method, which corresponds to the Predicate signature.

+28
Aug 28 '08 at 2:12
source share

As stated in "Learning C # 3.0: Learn the Basics of C # 3.0"

General scenario: When the head of state dies, the president of the United States usually does not have time to attend the funeral personally. Instead, he sends a delegate. Often this delegate is vice president, but sometimes the vice president is unavailable, and the president must send someone else, such as the secretary of state or even the first lady. He does not want to โ€œimpedeโ€ his delegated authority to one person; he can delegate this responsibility to anyone who can implement the correct international protocol.

The President determines in advance what responsibility will be delegated (to attend the funeral), what parameters will be accepted (condolences, kind words), and what value he hopes to return (goodwill). He then assigns a specific person to this delegated responsibility at "execution time" when his presidency is progressing.

When programming the scenario:. You often encounter situations where you need to perform a certain action, but you donโ€™t know in which method or even the object you want to call to advance it.

Example: The button may not know which object or objects need to be notified. Instead of attaching a button to a specific object, you connect the button to a delegate, and then allow those who delegate a particular method to the program.

+136
Jun 26 '13 at 10:43
source share

One common use of delegates for shared lists is through Action delegates (or its anonymous equivalent) to create a single-line foreach operation:

myList.Foreach( i => i.DoSomething()); 

I also found the Predicate delegate quite useful when searching or cropping a list:

 myList.FindAll( i => i.Name == "Bob"); myList.RemoveAll( i => i.Name == "Bob"); 

I know that you said no code is required, but itโ€™s easier for me to express my usefulness with the code. :)

+15
Aug 28 '08 at 2:32
source share

Binding events to event handlers is usually your first encounter with delegates ... You may not even know that you are using them because the delegate is completed in the EventHandler class.

+11
Aug 28 '08 at 2:00
source share

If you're interested in seeing how the Delegate pattern is used in real code, look no more than Cocoa on Mac OS X. Cocoa is the preferred UI toolkit for programming on Mac OS X and encoded in Objective C. It is designed so that every component the user interface is intended to be expanded by delegation, not a subclass or other means.

For more information, I recommend checking what Apple has to say about delegates here .

+4
Aug 28 '08 at 2:30
source share

I had a project that used win32 Python.

For various reasons, some modules used odbc.py to access the database and other modules - pyodbc.py.

There was a problem when the function was to be used by both types of modules. He had a connection object passed to him as an argument, but then he had to know whether to use dbi.dbiDate or datetime to represent the time.

This is because the expected odbc.py, as the values โ€‹โ€‹in the SQL statements, is dated as dbi.dbiDate, while the pyodbc.py is the expected datetime.

Another complication was that the connection objects created by odbc.py and pyodbc.py did not allow additional fields to be set.

My solution was to wrap the connection objects returned by odbc.odbc (...) and pyodbc.pyodbc (...) with a delegate class that contains the desired function to represent time as the value of an additional field, and which delegates all other field requests to the original connection object.

+4
Sep 16 '08 at 14:39
source share

I had the same question as you, and I went to this site to answer.

Apparently, I did not understand this better, although I was looking through examples in this thread.

I found great use for delegates now that I read: http://www.c-sharpcorner.com/UploadFile/thiagu304/passdata05172006234318PM/passdata.aspx

This might seem more obvious to new users, because Forms is much harder to pass values โ€‹โ€‹than ASP.NET websites with POST / GET (QueryString) ..

Basically you define a delegate that takes TextBox text as parameters.

// Form1

 // Class Property Definition public delegate void delPassData(TextBox text); // Click Handler private void btnSend_Click(object sender, System.EventArgs e) { Form2 frm= new Form2(); delPassData del=new delPassData(frm.funData); del(this.textBox1); frm.Show(); } 

// SUMMARY: defining a delegate, creating a new Form2 class, assigning the funData () function for delegation, passing it to a delegate in a text field. Show form.

// Form2

 public void passData(TextBox txtForm1) { label1.Text = txtForm1.Text; } 

// SUMMARY: just take TextBox txtForm1 as parameters (as defined in your delete) and assign label text to text text.

Hope this enlightens some use of delegates :) ..

+3
May 10 '10 at 8:42 a.m.
source share

A quick Google search came up with http://en.wikipedia.org/wiki/Delegation_pattern . Basically, anytime you use an object that redirects it to another object, you delegate.

0
Aug 28 '08 at 2:03
source share



All Articles