Reputation: 1785
What is the best way to pass parameters through to a delegate? I can see benefits for both of the following ways so I would like to know the most used and industry accepted way of doing it:
1) Pass any parameters individually with each parameters being just that, a parameter.
Example Delegate
public delegate void MyDelegate(bool PARAM1, String PARAM2, int PARAM3);
2) Pass any parameters through a struct and the only parameter of the delegate is that struct.
Example Struct
public struct MyDelegateArgs
{
public bool PARAM1;
public String PARAM2;
public int PARAM3;
}
Example Delegate
public delegate void MyDelegate(MyDelegateArgs args);
Upvotes: 1
Views: 148
Reputation: 7375
If your delegate has utility as an Action<T>
or a Predicate<T>
, using the struct argument is preferable as it will keep you from tripping over a case where the signature gets too large for the predefined Action
or Predicate
definitions when you go to extend the delegate's signature.
Upvotes: 1
Reputation: 160952
Imagine your struct had 20 properties / fields - would you really want to pass all of those in as parameters? Generally I would argue to hide all of this complexity and keep your code DRY by passing in a class / struct instead - this in most cases is also more expressive - your delegates needs a parameter of type MyDelegateArgs
.
The only argument I could come up with for passing in individual values is if you only need a small subset that just happen to be used in MyDelegateArgs
but are otherwise unrelated.
Upvotes: 2