onevent Code Snippet

When raising events, the .NET framework design guidelines recommend that this is done from a protected virtual method named On<EventName> (unless the class is sealed or the event is static, etc.). This leads to a recurring pattern of similarly looking code like this:

 protected virtual void OnMyEvent(MyEventArgs e)
{
    if (this.MyEvent != null)
    {
        this.MyEvent(this, e);
    }
}

After having done this numerous times it struck me as a good candidate for a code snippet, so I decided to create the onevent code snippet. Creating a code snippet is pretty easy; from my decision to create the snippet to using it for the first time, I spent less than half an hour. Here's how:

Create a new XML file. For the onevent code snippet, the contents look like this:

 <?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>onevent</Title>
            <Shortcut>onevent</Shortcut>
            <Description>Code snippet for creating a method for raising an event</Description>
            <Author>Mark Seemann</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>event</ID>
                    <ToolTip>The event to raise</ToolTip>
                    <Default>Event</Default>
                </Literal>
                <Literal>
                    <ID>eventargs</ID>
                    <ToolTip>The type of event args for the event</ToolTip>
                    <Default>EventArgs</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[protected virtual void On$event$($eventargs$ e)
    {
        if (this.$event$ != null)
        {
            this.$event$(this, e);
        }
    }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Each snippet consists of a header and the snippet itself. The header mostly contains metadata like the title, the shortcut used in the code editor, etc. The only vaguely puzzling element is the SnippetTypes element, where you must specify the type of code snippet. Expansion just indicates that the code snippet will be inserted at the current location of the cursor (see the Code Snippet Schema Reference at https://msdn2.microsoft.com/en-us/library/ms171418.aspx for a full description of the possible values).

The Snippet section of the XML file consists of a Declarations section and the actual code for the snippet. Declarations is used to specify the placeholders for your code expressions - in this case, the name of the event, and the type of the event args.

The actual code is defined in a CDATA section, where each declaration is referenced by its name pre- and postfixed with $.

To start using the code snippet, just save the XML file with the .snippet extension to the C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C# folder and type onevent followed by the Tab key in your code editor. Visual Studio picks up the new code snippet immediately, so you don't even have to restart the IDE!

Obviously, you need to have already defined the EventArgs class and the event itself, but with this in place you now have an instant event raiser method, which follows the framework design guidelines.

Update: Several people have been so kind to point out to me that my code is not thread-safe (see the comments to this post). For that, I humbly apologize. In a new comment, I've posted the updated XML for the code snippet, including the thread-safe code.

Additionally, I have also included the updated snippet as an attachment to the post, so now you can just download the file to your C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C# folder to begin using it.

onevent.snippet