<p>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.</p>

[{'answer_id': 31502, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 4, 'selected': False, 'text': '<p>Binding Events to Event Handlers is usually your first introduction to delegates...You might not even know you were using them because the delegate is wrapped up in the EventHandler class.</p>\n'}, {'answer_id': 31508, 'author': 'martinatime', 'author_id': 1353, 'author_profile': 'https://Stackoverflow.com/users/1353', 'pm_score': 1, 'selected': False, 'text': '<p>A quick google search came up with this <a href="http://en.wikipedia.org/wiki/Delegation_pattern" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Delegation_pattern</a> . Basically, anytime that you use an object that forwards it\'s calls to another object then you are delegating.</p>\n'}, {'answer_id': 31522, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 6, 'selected': True, 'text': '<blockquote>\n  <p>A delegate is a named type that defines a particular kind of method. Just as a class definition lays out all the members for the given kind of object it defines, the delegate lays out the method signature for the kind of method it defines.</p>\n</blockquote>\n\n<p>Based on this statement, a delegate is a function pointer and it defines what that function looks like.</p>\n\n<p>A great example for a real world application of a delegate is the <a href="http://msdn.microsoft.com/en-us/library/bfcke1bz.aspx" rel="noreferrer">Predicate</a>.  In the example from the link, you will notice that <a href="http://msdn.microsoft.com/en-us/library/d9hy2xwa.aspx" rel="noreferrer">Array.Find</a> takes the array to search and then a predicate to handle the criteria of what to find.  In this case it passes a method ProductGT10 which matches the Predicate signature.</p>\n'}, {'answer_id': 31542, 'author': 'spate', 'author_id': 2276, 'author_profile': 'https://Stackoverflow.com/users/2276', 'pm_score': 3, 'selected': False, 'text': '<p>If you\'re interested in seeing how the Delegate pattern is used in real-world code, look no further than Cocoa on Mac OS X.  Cocoa is Apple\'s preferred UI toolkit for programming under Mac OS X, and is coded in Objective C.  It\'s designed so that each UI component is intended to be extended via delegation rather than subclassing or other means.</p>\n\n<p>For more information, I recommend checking out what Apple has to say about delegates <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_4.html" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 31549, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 4, 'selected': False, 'text': '<p>One common use of delegates for generic Lists are via Action delegates (or its anonymous equivalent) to create a one-line foreach operation:</p>\n\n<pre><code>myList.Foreach( i =&gt; i.DoSomething());\n</code></pre>\n\n<p>I also find the Predicate delegate quite useful in searching or pruning a List:</p>\n\n<pre><code>myList.FindAll( i =&gt; i.Name == "Bob");    \nmyList.RemoveAll( i =&gt; i.Name == "Bob");\n</code></pre>\n\n<p>I know you said no code required, but I find it easier to express its usefulness via code. :)</p>\n'}, {'answer_id': 73046, 'author': 'Omer Zak', 'author_id': 11886, 'author_profile': 'https://Stackoverflow.com/users/11886', 'pm_score': 3, 'selected': False, 'text': '<p>I had a project which used win32 Python.  </p>\n\n<p>Due to various reasons, some modules used odbc.py to access the DB, and other modules - pyodbc.py.  </p>\n\n<p>There was a problem when a function needed to be used by both kinds of modules.  It had an connection object passed to it as an argument, but then it had to know whether to use dbi.dbiDate or datetime to represent times.  </p>\n\n<p>This was because odbc.py expected, as values in SQL statements, dates as dbi.dbiDate whereas pyodbc.py expected datetime values.</p>\n\n<p>One further complication was that the connection objects created by odbc.py and pyodbc.py did not allow one to set additional fields.</p>\n\n<p>My solution was to wrap the connection objects returned by odbc.odbc(...) and pyodbc.pyodbc(...) by a delegate class, which contains the desired time representation function as the value of an extra field, and which delegates all other field requests to the original connection object.</p>\n'}, {'answer_id': 2801349, 'author': 'dza', 'author_id': 169854, 'author_profile': 'https://Stackoverflow.com/users/169854', 'pm_score': 3, 'selected': False, 'text': '<p>I had the same question as you and went to this site for an answer.</p>\n\n<p>Apparently, I didn\'t understood it better even though I skimmed through the examples on this thread.</p>\n\n<p><strong>I found a great use for delegates now that I read:</strong> <a href="http://www.c-sharpcorner.com/UploadFile/thiagu304/passdata05172006234318PM/passdata.aspx" rel="noreferrer">http://www.c-sharpcorner.com/UploadFile/thiagu304/passdata05172006234318PM/passdata.aspx</a></p>\n\n<p>This might seem more obvious for new users because Forms is much more complicated to pass values than ASP.NET websites with POST/GET (QueryString) ..</p>\n\n<p>Basically you define a delegate which takes <strong>"TextBox <em>text</em>"</strong> as parameters.</p>\n\n<p>// Form1</p>\n\n<pre><code>// Class Property Definition\npublic delegate void delPassData(TextBox text);\n\n\n// Click Handler\nprivate void btnSend_Click(object sender, System.EventArgs e)\n{\n     Form2 frm= new Form2();\n     delPassData del=new delPassData(frm.funData);\n     del(this.textBox1);\n     frm.Show();\n}\n</code></pre>\n\n<p>// SUMMARY: Define delegate, instantiate new Form2 class, assign funData() function to delegate, pass in your textBox to the delegate. Show the form.</p>\n\n<p>// Form2</p>\n\n<pre><code>public void passData(TextBox txtForm1)\n{\n\n     label1.Text = txtForm1.Text;\n}\n</code></pre>\n\n<p>// SUMMARY: Simply take TextBox txtForm1 as parameters (as defined in your delegate) and assign label text to textBox\'s text.</p>\n\n<p>I hope this enlightens some use on delegates :) ..</p>\n'}, {'answer_id': 17317903, 'author': 'Atish Dipongkor', 'author_id': 1294606, 'author_profile': 'https://Stackoverflow.com/users/1294606', 'pm_score': 7, 'selected': False, 'text': '<p>As stated in <a href="http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-17.aspx" rel="noreferrer">&quot;Learning C# 3.0: Master the fundamentals of C# 3.0&quot;</a></p>\n<blockquote>\n<p><strong>General Scenario:</strong>   When a head of state dies, the President of the United States typically does not have time to attend the funeral\npersonally. Instead, he dispatches a delegate. Often this delegate is\nthe Vice President, but sometimes the VP is unavailable and the\nPresident must send someone else, such as the Secretary of State or\neven the First Lady. He does not want to “hardwire” his delegated\nauthority to a single person; he might delegate this responsibility to\nanyone who is able to execute the correct international protocol.</p>\n<p>The President defines in advance what responsibility will be delegated\n(attend the funeral), what parameters will be passed (condolences,\nkind words), and what value he hopes to get back (good will). He then\nassigns a particular person to that delegated responsibility at\n“runtime” as the course of his presidency progresses.</p>\n<p><strong>In programming Scenario:</strong>   You are often faced with situations where you need to execute a particular action, but you don’t know in\nadvance which method, or even which object, you’ll want to call upon\nto execute it.</p>\n<p><strong>For Example:</strong>   A button might not know which object or objects need to be notified. Rather than wiring the button to a particular\nobject, you will connect the button to a delegate and then resolve\nthat delegate to a particular method when the program executes.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/31497', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1635/']