With generics, is there ever a reason to create specific derived EventArg classes
It seems like now you can simply use them on the fly with a generic implementation.
Look at the Custom Generic EventArgs article written by Matthew Cochran, in that article he describes how to expand it even further with two and three members.
Using generic EventArgs have their uses, and of course their misuses, as type information is lost in the process.
public class City {...}
public delegate void FireNuclearMissile(object sender, EventArgs args);
public event FireNuclearMissile FireNuclearMissileEvent;
public delegate void QueryPopulation(object sender, EventArgs args);
public event QueryPopulation QueryPopulationEvent;
In the following example it is type-safe, but a bit more LOC:
class City {...}
public class FireNuclearMissileEventArgs : EventArgs
{
public FireNuclearMissileEventArgs(City city)
{
this.city = city;
}
private City city;
public City City
{
get { return this.city; }
}
}
public delegate void FireNuclearMissile(object sender, FireNuclearMissileEventArgs args);
public event FireNuclearMissile FireNuclearMissileEvent;
public class QueryPopulationEventArgs : EventArgs
{
public QueryPopulationEventArgs(City city)
{
this.city = city;
}
private City city;
public City City
{
get { return this.city; }
}
}
public delegate void QueryPopulation(object sender, QueryPopulationEventArgs args);
public event QueryPopulation QueryPopulationEvent;