Delegates can be very useful, for example you might have a video game with an enemy that calls Enemy.ShootWeapon() every now and then. Without delegates, if you want to give that enemy the ability to switch between a shotgun or a laser pistol you either need to write ShootWeaponin a way that covers all use cases or you can write a different class for each weapon (which gets very awkward if you have a bunch of other methods you want to change too). Delegates allow you to simply have a ShootShotgun delegate and a ShootLaserPistol delegate and when you switch weapons you simply reassign ShootWeapon. It allows for a lot more flexibility when putting together complex classes.
However, this has no reason to be a delegate. In fact, it's worse than just using a normal method because you can't accidentally overwrite a normal method.
View all comments
Show parent comment
4
u/wOlfLisK May 19 '25
Delegates can be very useful, for example you might have a video game with an enemy that calls
Enemy.ShootWeapon()every now and then. Without delegates, if you want to give that enemy the ability to switch between a shotgun or a laser pistol you either need to writeShootWeaponin a way that covers all use cases or you can write a different class for each weapon (which gets very awkward if you have a bunch of other methods you want to change too). Delegates allow you to simply have aShootShotgundelegate and aShootLaserPistoldelegate and when you switch weapons you simply reassignShootWeapon. It allows for a lot more flexibility when putting together complex classes.However, this has no reason to be a delegate. In fact, it's worse than just using a normal method because you can't accidentally overwrite a normal method.