Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will an empty delegate eat up memory?

public sealed class FtpManager
{
    public event EventHandler LoggingIn = delegate { };
    private void OnLoggingIn(object sender, EventArgs e)
    {
        var handler = LoggingIn;
        handler(sender, e);
    }
// ...
}

In the above code, I have initialized LoggingIn event handler with an empty delegate.

Will that affect memory space used in any way? Especially when there are hundreds or thousands of events declared such way?

like image 908
dance2die Avatar asked Mar 31 '09 20:03

dance2die


People also ask

What are EventArgs?

EventArgs is also the class you use when an event does not have any data associated with it. When you create an event that is only meant to notify other classes that something happened and does not need to pass any data, include the EventArgs class as the second parameter in the delegate. You can pass the EventArgs.

Why do we need events when we have delegates?

Yes, Events! This is a simplified explanation on why we use events even though we can already use delegates: To provide encapsulation and not exposing business logic. To prevent Team Client from clearing all assign methods to delegates (You cannot do that for events):

How event handler works in C#?

An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections, raised by controls in the user interface.

How to raise event in C#?

Raising an events is a simple step. First you check the event agaist a null value to ensure that the caller has registered with the event, and then you fire the event by specifying the event by name as well as any required parameters as defined by the associated delegate. MyEvent(message);


1 Answers

Scratch the previous answer (kept below for posterity). It depends on the implementation of the compiler, but under the current MS C# 3.0 compiler, this actually only creates a single instance which is reused for every instance. It's able to do that because delegates are immutable and that delegate doesn't require any information from the instance.

I don't know whether this was the case with C# 2.0 though. You can decompile your code and see whether the IL actually uses a cached field or not. Using the answer below is a safe way to guarantee you'll only create one instance though.

Original answer:

Yes, it creates an instance of a delegate. That will take some memory. You could reduce that though:

public static class EventHandlers
{
    public static readonly EventHandler Empty = delegate {};
}

public sealed class FtpManager
{
    public event EventHandler LoggingIn = EventHandlers.Empty;
}

At that point there'll just be the one instance, and you can refer to it from anywhere. The downside is that other classes could then unsubscribe using the same handler. If you trust the rest of your codebase not to do that, this is probably the best bet from a memory point of view.

like image 103
Jon Skeet Avatar answered Oct 16 '22 07:10

Jon Skeet