When would you use delegates in C#? [closed]

Now that we have lambda expressions and anonymous methods in C#, I use delegates much more. In C# 1, where you always had to have a separate method to implement the logic, using a delegate often didn’t make sense. These days I use delegates for: Event handlers (for GUI and more) Starting threads Callbacks (e.g. … Read more

How to add a delegate to an interface C#

Those are declaring delegate types. They don’t belong in an interface. The events using those delegate types are fine to be in the interface though: public delegate void UpdateStatusEventHandler(string status); public delegate void StartedEventHandler(); public interface IMyInterface { event UpdateStatusEventHandler StatusUpdated; event StartedEventHandler Started; } The implementation won’t (and shouldn’t) redeclare the delegate type, any … Read more

Difference between events and delegates and its respective applications [closed]

The keyword event is a scope modifier for multicast delegates. Practical differences between this and just declaring a multicast delegate are as follows: You can use event in an interface. Invocation access to the multicast delegate is limited to the declaring class. The behaviour is as though the delegate were private for invocation. For the … Read more

Where do I use delegates? [closed]

As stated in “Learning C# 3.0: Master the fundamentals of C# 3.0” General Scenario: When a head of state dies, the President of the United States typically does not have time to attend the funeral personally. Instead, he dispatches a delegate. Often this delegate is the Vice President, but sometimes the VP is unavailable and … Read more

C# pattern to prevent an event handler hooked twice [duplicate]

How about just removing the event first with -= , if it is not found an exception is not thrown /// -= Removes the event if it has been already added, this prevents multiple firing of the event ((System.Windows.Forms.WebBrowser)sender).Document.Click -= new System.Windows.Forms.HtmlElementEventHandler(testii); ((System.Windows.Forms.WebBrowser)sender).Document.Click += new System.Windows.Forms.HtmlElementEventHandler(testii);

Are C# events synchronous?

This is a general answer and reflects the default behavior: Yes, it blocks the thread, if the methods subscribing to the event are not asynchronous. They are executed one after the other. This has another twist: If one event handler throws an exception, the event handlers not yet executed will not be executed. Having said … Read more

“Delegate subtraction has unpredictable result” in ReSharper/C#?

Don’t be afraid! The first part of ReSharper’s warning only applies to removing lists of delegates. In your code, you’re always removing a single delegate. The second part talks about ordering of delegates after a duplicate delegate was removed. An event doesn’t guarantee an order of execution for its subscribers, so it doesn’t really affect … Read more

Why must a lambda expression be cast when supplied as a plain Delegate parameter

A lambda expression can either be converted to a delegate type or an expression tree – but it has to know which delegate type. Just knowing the signature isn’t enough. For instance, suppose I have: public delegate void Action1(); public delegate void Action2(); … Delegate x = () => Console.WriteLine(“hi”); What would you expect the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)