Performance of calling delegates vs methods

I haven’t seen that effect – I’ve certainly never encountered it being a bottleneck. Here’s a very rough-and-ready benchmark which shows (on my box anyway) delegates actually being faster than interfaces: using System; using System.Diagnostics; interface IFoo { int Foo(int x); } class Program : IFoo { const int Iterations = 1000000000; public int Foo(int … Read more

C# Generics won’t allow Delegate Type Constraints

A number of classes are unavailable as generic contraints – Enum being another. For delegates, the closest you can get is “: class”, perhaps using reflection to check (for example, in the static constructor) that the T is a delegate: static GenericCollection() { if (!typeof(T).IsSubclassOf(typeof(Delegate))) { throw new InvalidOperationException(typeof(T).Name + ” is not a delegate … Read more

Is there a downside to adding an anonymous empty delegate on event declaration?

Instead of inducing performance overhead, why not use an extension method to alleviate both problems: public static void Raise(this EventHandler handler, object sender, EventArgs e) { if(handler != null) { handler(sender, e); } } Once defined, you never have to do another null event check again: // Works, even for null events. MyButtonClick.Raise(this, EventArgs.Empty);

What’s the difference between ‘weak’ and ‘assign’ in delegate property declaration

The only difference between weak and assign is that if the object a weak property points to is deallocated, then the value of the weak pointer will be set to nil, so that you never run the risk of accessing garbage. If you use assign, that won’t happen, so if the object gets deallocated from … Read more

Proper naming convention for a .NET Delegate type? [closed]

Personally I use a couple of different patterns: [Task][State]Handler – UITaskFinishedHandler [Event]Handler – ControlLoadedHandler [Function Name]Delegate – DoSomeWorkDelegate – used when I need to create a delegate for calling a function on a different/new thread [Task]Callback – ContainerLoadedCallback – used when control A starts an action which control B does most of the work and … Read more

Wrapping StopWatch timing with a delegate or lambda?

How about extending the Stopwatch class? public static class StopwatchExtensions { public static long Time(this Stopwatch sw, Action action, int iterations) { sw.Reset(); sw.Start(); for (int i = 0; i < iterations; i++) { action(); } sw.Stop(); return sw.ElapsedMilliseconds; } } Then call it like this: var s = new Stopwatch(); Console.WriteLine(s.Time(() => DoStuff(), 1000)); … Read more

Invoke(Delegate)

The answer to this question lies in how C# Controls work Controls in Windows Forms are bound to a specific thread and are not thread safe. Therefore, if you are calling a control’s method from a different thread, you must use one of the control’s invoke methods to marshal the call to the proper thread. … Read more

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