delegates

What makes FSharpFunc<> faster than Func<>?

烂漫一生 提交于 2019-12-18 12:10:49
问题 I'm curious about the performance enhancements that have been made for FSharpFunc<>. Is it the fact that it does not contain multiple delegate so there is no need to loop over all the references when firing a function call ? Anything else ? 回答1: I think that the primary motivation for using FSharpFunc<> rather than Func<> or any other delegate is that you cannot create a class that would inherit from a delegate type (at first, this sounds reasonable, but in .NET, delegate is actually just

Creating a delegate type inside a method

一笑奈何 提交于 2019-12-18 11:46:02
问题 I want to create a delegate type in C# inside a method for the purpose of creating Anonymous methods. For example: public void MyMethod(){ delegate int Sum(int a, int b); Sum mySumImplementation=delegate (int a, int b) {return a+b;} Console.WriteLine(mySumImplementation(1,1).ToString()); } Unfortunately, I cannot do it using .NET 2.0 and C# 2.0. 回答1: Why do you want to create the delegate type within the method? What's wrong with declaring it outside the method? Basically, you can't do this -

Does assigning null remove all event handlers from an object?

[亡魂溺海] 提交于 2019-12-18 11:41:47
问题 I have defined new member in my class protected COMObject.Call call_ = null; This class has the following event handler that I subscribed to call_.Destructed += new COMObject.DestructedEventHandler(CallDestructedEvent); Will setting my member to null as following remove the event handler? call_ = null; or I have to unsubscribed with -=? 回答1: yes, you should use overloaded -= to unsubscribe an event. simply assigning a reference to null will not do that automatically. The object will still be

Typesafe fire-and-forget asynchronous delegate invocation in C#

懵懂的女人 提交于 2019-12-18 11:32:09
问题 I recently found myself needing a typesafe "fire-and-forget" mechanism for running code asynchronously. Ideally, what I would want to do is something like: var myAction = (Action)(() => Console.WriteLine("yada yada")); myAction.FireAndForget(); // async invocation Unfortunately, the obvious choice of calling BeginInvoke() without a corresponding EndInvoke() does not work - it results in a slow resource leak (since the asyn state is held by the runtime and never released ... it's expecting an

Typesafe fire-and-forget asynchronous delegate invocation in C#

北城余情 提交于 2019-12-18 11:31:15
问题 I recently found myself needing a typesafe "fire-and-forget" mechanism for running code asynchronously. Ideally, what I would want to do is something like: var myAction = (Action)(() => Console.WriteLine("yada yada")); myAction.FireAndForget(); // async invocation Unfortunately, the obvious choice of calling BeginInvoke() without a corresponding EndInvoke() does not work - it results in a slow resource leak (since the asyn state is held by the runtime and never released ... it's expecting an

C# - ThreadPool QueueUserWorkItem Use?

ぃ、小莉子 提交于 2019-12-18 11:10:50
问题 Just right now I'm using following code to add queued threads. I don't like it. And my colleagues won't either because they don't know C# very well. All I want is of course to queue a method to be executed in a new thread. private static void doStuff(string parameter) { // does stuff } // call (a) ThreadPool.QueueUserWorkItem(a => doStuff("hello world")); // call (b) ThreadPool.QueueUserWorkItem(delegate { doStuff("hello world"); }); So are there other use variations of ThreadPool

Avoid duplicate event subscriptions in C#

对着背影说爱祢 提交于 2019-12-18 11:06:36
问题 How would you suggest the best way of avoiding duplicate event subscriptions? if this line of code executes in two places, the event will get ran twice. I'm trying to avoid 3rd party events from subscribing twice. theOBject.TheEvent += RunMyCode; In my delegate setter, I can effectively run this ... theOBject.TheEvent -= RunMyCode; theOBject.TheEvent += RunMyCode; but is that the best way? 回答1: I think, the most efficient way, is to make your event a property and add concurrency locks to it

Why does trying to understand delegates feel like trying to understand the nature of the universe?

試著忘記壹切 提交于 2019-12-18 10:07:41
问题 I've read two books, tons of examples. They still make next to no sense to me. I could probably write some code that uses delegates, but I have no idea why. Am I the only one with this problem, or am I just an idiot? If anyone can actually explain to me when, where, and why I would actually use a delegate, I'll love you forever. 回答1: Delegates are just a way to pass around a function in a variable. You pass a delegated function to do a callback. Such as when doing asynchronous IO, you pass a

How to create an asynchronous method

百般思念 提交于 2019-12-18 09:59:41
问题 I have simple method in my C# app, it picks file from FTP server and parses it and stores the data in DB. I want it to be asynchronous, so that user perform other operations on App, once parsing is done he has to get message stating "Parsing is done". I know it can achieved through asynchronous method call but I dont know how to do that can anybody help me please?? 回答1: You need to use delegates and the BeginInvoke method that they contain to run another method asynchronously. A the end of

How do you use Func<> and Action<> when designing applications?

我只是一个虾纸丫 提交于 2019-12-18 09:54:19
问题 All the examples I can find about Func<> and Action<> are simple as in the one below where you see how they technically work but I would like to see them used in examples where they solve problems that previously could not be solved or could be solved only in a more complex way, i.e. I know how they work and I can see they are terse and powerful , so I want to understand them in a larger sense of what kinds of problems they solve and how I could use them in the design of applications. In what