I have a class which needs to provide a way to register an arbitrary number of Func<T, TResult> delegates.
These are then later invoked in my class during a series of operations.
What is the correct way to expose the ability to register these delegates? Is it just a public property of IEnumerable<Func<T, TResult>>, IList<Func<T, TResult>>, ReadOnlyCollection<Func<T, TResult>>?
Is there some specific pattern that is typically used here?
To be more specific..
Assume a scenario
public class Foo
{
public List<Func<Control, Func<bool>>> Conditions { get; set; }
public void DoWork()
{
foreach (Func<Control, Func<bool>> condition in this.Conditions)
var result = condition.Invoke(new Control());
}
}
Is this an acceptable implementation?
There’s not enough information. It will depend completely on your situation, particularly on the contractual guarantees you want to provide (e.g. guaranteed order, immutability of the collection, etc.). Unless you have special requirements, I suggest you just go with the simplest version —
IEnumerable<Func<T, TResult>>— and leave the rest to your implementation. That’s what I’ve done in the past.As for using a
Listas in your example, there’s nothing intrinsically wrong with that. You may find it convenient to stick withIEnumerable<Func<T, TResult>>so as not to restrict yourself more than necessary.List<T>implementsIEnumerable<T>, so you can still useConditions = new List<Func<T, TResult>>when you initialize it.