EventArgs Class
Collapse the table of content
Expand the table of content

EventArgs Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

EventArgs is the base class for classes containing event data.


Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public class EventArgs

The EventArgs type exposes the following members.

Public methodEventArgsInitializes a new instance of the EventArgs class.

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public fieldStatic memberEmptyRepresents an event with no event data.

This class contains no event data; it is used by events that do not pass state information to an event handler when an event is raised. If the event handler requires state information, the application must derive a class from this class to hold the data.

For more information about events, see the EventHandler topic.

The following code sample illustrates the use of EventArgs.

In this sample, FireEventArgs is a set of event arguments derived from EventArgs, and passed to the FireEventHandler when an event is raised by calling ActivateFireAlarm.


For information on how to compile and run this example code, see Building examples that have static TextBlock controls for Windows Phone 8.

using System;
using System.Windows.Controls;

// FireEventArgs: a custom event inherited from EventArgs.

public class FireEventArgs : EventArgs
    public FireEventArgs(string room, int ferocity)
        this.room = room;
        this.ferocity = ferocity;

    // The fire event will have two pieces of information--
    // 1) Where the fire is, and 2) how "ferocious" it is.

    public string room;
    public int ferocity;


// Class with a function that creates the eventargs and initiates the event
public class FireAlarm

    // Events are handled with delegates, so we must establish a FireEventHandler
    // as a delegate:

    public delegate void FireEventHandler(object sender, FireEventArgs fe);

    // Now, create a public event "FireEvent" whose type is our FireEventHandler delegate.

    public event FireEventHandler FireEvent;

    // This will be the starting point of our event-- it will create FireEventArgs,
    // and then raise the event, passing FireEventArgs.

    public void ActivateFireAlarm(string room, int ferocity)

        FireEventArgs fireArgs = new FireEventArgs(room, ferocity);

        // Now, raise the event by invoking the delegate. Pass in
        // the object that initated the event (this) as well as FireEventArgs.
        // The call must match the signature of FireEventHandler.

        FireEvent(this, fireArgs);

// Class which handles the event

class FireHandlerClass

    private static TextBlock _textblock;

    // Create a FireAlarm to handle and raise the fire events.

    public FireHandlerClass(FireAlarm fireAlarm, TextBlock outputBlock)

        // Add a delegate containing the ExtinguishFire function to the class'
        // event so that when FireAlarm is raised, it will subsequently execute
        // ExtinguishFire.

        fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);

        FireHandlerClass._textblock = outputBlock;


    // This is the function to be executed when a fire event is raised.

    void ExtinguishFire(object sender, FireEventArgs fe)

        _textblock.Text += String.Format("\nThe ExtinguishFire function was called by {0}.", sender.ToString()) + "\n";

        // Now, act in response to the event.

        if (fe.ferocity < 2)
            _textblock.Text += String.Format("This fire in the {0} is no problem.  I'm going to pour some water on it.", fe.room) + "\n";
        else if (fe.ferocity < 5)
            _textblock.Text += String.Format("I'm using FireExtinguisher to put out the fire in the {0}.", fe.room) + "\n";
            _textblock.Text += String.Format("The fire in the {0} is out of control.  I'm calling the fire department!", fe.room) + "\n";

public class Example
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)

        // Create an instance of the class that will be firing an event.

        FireAlarm myFireAlarm = new FireAlarm();

        // Create an instance of the class that will be handling the event. Note that
        // it receives the class that will fire the event as a parameter.

        FireHandlerClass myFireHandler = new FireHandlerClass(myFireAlarm, outputBlock);

        //use our class to raise a few events and watch them get handled
        myFireAlarm.ActivateFireAlarm("Kitchen", 3);
        myFireAlarm.ActivateFireAlarm("Study", 1);
        myFireAlarm.ActivateFireAlarm("Porch", 5);




Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


© 2017 Microsoft