Reputation: 21
I have Action<string, string>
, but in subscribed method I don't need that data. Can I subscribe method without those arguments somehow? I just don't want to make useless arguments. And I cant just make that action's data blank, since it's needed in other script
public Action<string, string> onKill;
Other script
health.OnKill += AddArmor; // Here I want to make AddArmor argumentless, but because of delegate, I can't
Upvotes: 0
Views: 263
Reputation: 174
C#, you can use the _ (underscore) symbol to ignore arguments in delegates HERE in your case Yes, you can use a lambda expression to ignore the arguments when subscribing the method to the delegate.
health.OnKill += (_, __) => AddArmor();
Here, the lambda expression ignores both arguments from the Action<string, string> delegate by assigning them to _ and __ respectively. The AddArmor method is then invoked without any arguments.
Upvotes: 1
Reputation: 62002
The usual thing to do is to wrap it in a small lambda:
health.OnKill += (x, y) => AddArmor();
The old syntax for anonymous function offers to leave out x
and y
altogether:
health.OnKill += delegate { AddArmor(); };
Upvotes: 1