Equivalent of typedef in C#

Posted by Matthew Scharley on Stack Overflow See other posts from Stack Overflow or by Matthew Scharley
Published on 2008-10-02T09:18:29Z Indexed on 2010/03/21 10:51 UTC
Read the original article Hit count: 577

Filed under:
|

Is there a typedef equivalent in C#, or someway to get some sort of similar behaviour? I've done some googling, but everywhere I look seems to be negative. Currently I have a situation similar to the following:

class GenericClass<T> 
{
    public event EventHandler<EventData> MyEvent;
    public class EventData : EventArgs { /* snip */ }
    // ... snip
}

Now, it doesn't take a rocket scientist to figure out that this can very quickly lead to alot of typing (appologies for the horrible pun) when trying to implement a handler for that event. It'd end up being something like this:

GenericClass<int> gcInt = new GenericClass<int>;
gcInt.MyEvent += new EventHandler<GenericClass<int>.EventData>(gcInt_MyEvent);
// ...

private void gcInt_MyEvent(object sender, GenericClass<int>.EventData e)
{
    throw new NotImplementedException();
}

Except, in my case, I was already using a complex type, not just an int. It'd be nice if it was possible to simplify this a little...

Edit: ie. perhaps typedefing the EventHandler instead of needing to redefine it to get similar behaviour.

© Stack Overflow or respective owner

Related posts about c#

Related posts about typedef