Click here to Skip to main content
12,953,541 members (50,395 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 6 Sep 2012

Referencing events in C# using interfaces - A simple approach

, 6 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Passing event references as interfaces in C# by proxifying them within "EventReference" instances.


The following article shows a simple method to "reference" events using interfaces and generics. For the sake of simplicity, it uses Actions and Functions.


The only closely related article I could find is the following:

Source Code Overview

A non-generic event reference description:

public interface IEventReference { }

can be used to constrain the more concrete, but nevertheless generic event description interfaces.

public interface IActionEventReference<I> where I : IEventReference
    event Action Event;
public interface IActionEventReference<I, P1> where I : IEventReference
    event Action<P1> Event;

public interface IFuncEventReference<I, R> where I : IEventReference
    event Func<R> Event;
public interface IFuncEventReference<I, P1, R> where I : IEventReference
    event Func<P1, R> Event;

Using the Code  

Consider having a class which exposes some events, for example:

public class ClassExposingEvents
  public event Func<bool> BeforeCancel;

Adding the possibility to "reference" the BeforeCancel-event can be done in three steps:

First, an interface for the concrete event reference can be created: 

public interface IBeforeCancelEventReference : IEventReference { event Func<bool> BeforeCancel; }

and added to the class which provides the event:

pulic class ClassExposingEvents : IBeforeCancelEventReference
  public event Func<bool> BeforeCancel;

Second, a proxy class for the event can be defined (I usually do it as a subclass of the event container but this is not necessary):

public class BeforeCancelEventRef : 
         IFuncEventReference<IBeforeCancelEventReference, bool>
  IBeforeCancelEventReference e;
  public BeforeCancelEventRef(IBeforeCancelEventReference eventContainer) { e = eventContainer; }
  public event Func<bool> Event
    add { e.BeforeCancel += value; }
    remove { e.BeforeCancel -= value; }

Finally, all which need a reference to the BeforeCancel-event just need an instance of the class BeforeCancelEventRef (usually given back as an interface and created by a factory or something else which is appropriate).

To use the reference, we then can just add and remove targets from the Event-property of the reference class:

IFuncEventReference<IBeforeCancelEventReference, bool> bce = ...;
bce.Event += ...;

Points of Interest  

Exceptions and performance are not covered by the article.  


  • 2012/09/06: First version submitted.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Switzerland Switzerland
experienced in asm/c/c++/c#. Less experienced in Python, Lua, Shell and stuff like ASP, PHP, HTML ...
coding for windows, macOS, iOS and linux
just4fun: ogre3d

You may also be interested in...


Comments and Discussions

QuestionEvents Interface for Mdi and dinamically loaded childs Pin
angelo1110-Dec-13 3:49
memberangelo1110-Dec-13 3:49 
AnswerRe: Events Interface for Mdi and dinamically loaded childs Pin
zaphoed11-Dec-13 6:46
memberzaphoed11-Dec-13 6:46 
GeneralRe: Events Interface for Mdi and dinamically loaded childs Pin
AngelBlueSky16-Apr-15 21:57
memberAngelBlueSky16-Apr-15 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 6 Sep 2012
Article Copyright 2012 by zaphoed
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid