You can't use switch
in your scenario.
switch
statement requires an integral type or string as parameter (more details below). You can't pass EventArgs
as parameter to your switch statement, Also case
statement requires a compile time constant value.
If you look at the C# 5.0 Specifications:
The governing type of a switch statement is established by the switch
expression.
- If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, bool, char, string, or an enum-type, or if it
is the nullable type corresponding to one of these types, then that
is the governing type of the switch statement.
- Otherwise, exactly one user-defined implicit conversion (§6.4) must exist from the type of the switch expression to one of the following
possible governing types: sbyte, byte, short, ushort, int, uint,
long, ulong, char, string, or, a nullable type corresponding to one
of those types.
- Otherwise, if no such implicit conversion exists, or if more than one such implicit conversion exists, a compile-time error
occurs.
The other issue in your code is the case statement:
switch C#
Each case label specifies a constant value.
Your case statement specifies a value which can't be determined at a compile time.
This leaves you with the option of using if-else
block. Unless you are handling hundreds of types of EventArgs
, it is highly unlikely that you will see any significant performance gain.