delegates

What is the difference between using a delegate and using Func<T>/Action<T> in a method signature?

£可爱£侵袭症+ 提交于 2019-12-29 07:26:31
问题 I have been trying to get my head around delegates in C#, but I just don't seem to get the point of using them. Here is some slightly reconstructed code from the MSDN page on delegates: using System; using System.Collections; namespace Delegates { // Describes a book in the book list: public struct Book { public string Title; // Title of the book. public string Author; // Author of the book. public decimal Price; // Price of the book. public bool Paperback; // Is it paperback? public Book

How do i exit a List<string>.ForEach loop when using an anonymous delegate?

会有一股神秘感。 提交于 2019-12-29 04:27:13
问题 In a normal loop you can break out of a loop using break. Can the same be done using an anonymous delegate? Example inputString and result are both declared outside the delegate. blackList.ForEach(new Action<string>( delegate(string item) { if(inputString.Contains(item)==true) { result = true; // I want to break here } } )); Edit: Thanks for the replies, I'm actually reading your book at the minute John :) Just for the record i hit this issue and switched back to a normal foreach loop but I

Multiple Delegates in iOS

半腔热情 提交于 2019-12-29 04:12:15
问题 I am making an object that goes to download stuff for all of my view controllers. The object is singleton instance and has a callback method with received data once the download is completed. It also has a delegate property so that it knows which object to call back to after the download is done. There are multiple controllers that use this shared instance, and my question is how to call back to the correct view controller that requested the download. My approach is to use delegation, but the

In .NET, what is the internal implementation of a delegate?

孤人 提交于 2019-12-29 03:30:07
问题 I understand that a declaration of a delegate is something like this: public delegate int PerformCalculation(int x, int y); However, there must be more going on. The purpose of the delegate is to provide a pointer to a method, and to do that you encapsulate the reference to the method in the delegate. What kind of structure is this reference held in (internally in the delegate)? I also understand that you can encapsulate a reference to multiple methods in a delegate. Does this mean that there

Is there any way to delegate the event one in jQuery?

China☆狼群 提交于 2019-12-29 01:46:06
问题 I would like to delegate the event one for the click. Does anyone know if it is possible to do it? 回答1: I'm going to assume that you want the event to fire only once PER matched element rather than unbind entirely on the first click. I'd implement it like so: $('#container').delegate('.children', 'click', function() { if($(this).data('clicked')) { return; } // ... your code here ... $(this).data('clicked', true); }); This will fire only once per element. Technically, it fires everytime but is

Does Func<T>.BeginInvoke use the ThreadPool?

一个人想着一个人 提交于 2019-12-28 13:42:05
问题 When you call the BeginInvoke method on a Func delegates (or the Action delegates for that matter) in C#, does the runtime use the ThreadPool or spawn a new thread? I'm almost certain that it'll use the ThreadPool as that'd be the logical thing to do but would appreciate it if someone could confirm this. Thanks, 回答1: It uses the thread pool, definitely. I'm blowed if I can find that documented anyway, mind you... this MSDN article indicates that any callback you specify will be executed on a

Get current view controller from the app delegate (modal is possible)

和自甴很熟 提交于 2019-12-28 12:08:28
问题 I know that to get the current view controller from the app delegate, I can use the navigationController property I have set up for my app. However, it's possible in many places throughout my app that a modal navigation controller could have been presented. Is there any way to detect this from the app delegate, since the current navigation controller will be different from the one to which the app delegate holds a reference? 回答1: I suggest you use NSNofiticationCenter. //in AppDelegate:

“Uncurrying” an instance method in .NET

杀马特。学长 韩版系。学妹 提交于 2019-12-28 11:56:22
问题 Can you create a delegate of an instance method without specifying the instance at creation time? In other words, can you create a "static" delegate that takes as it's first parameter the instance the method should be called on? For example, how can I construct the following delegate using reflection? Func<int, string> = i=>i.ToString(); I'm aware of the fact that I can use methodInfo.Invoke, but this is slower, and does not check for type-correctness until it is called. When you have the

In .NET, what thread will Events be handled in?

房东的猫 提交于 2019-12-28 11:49:31
问题 I have attempted to implement a producer/consumer pattern in c#. I have a consumer thread that monitors a shared queue, and a producer thread that places items onto the shared queue. The producer thread is subscribed to receive data...that is, it has an event handler, and just sits around and waits for an OnData event to fire (the data is being sent from a 3rd party api). When it gets the data, it sticks it on the queue so the consumer can handle it. When the OnData event does fire in the

Does using delegates slow down my .NET programs?

本小妞迷上赌 提交于 2019-12-28 06:30:04
问题 Does using delegates slow down my programs? I've been avoiding them because I really have no clue if they make my programs any slower. I know if I cause a (catch) exception, that uses quite a bit of CPU power but I don't know about Delegates and Events and what .NET does to them. 回答1: Delegates are very, very fast. Not quite as fast as direct method calls, but not far off. The chances of them becoming a bottleneck are miniscule. (Likewise exceptions, when used properly, rarely actually cause