I’m not having much luck searching for this answer, as I think that I don’t know enough about the proper terms for it.
(Edit for clarity of code and how I call it)
I have a class that instantiates an extension method:
public static class Foo
{
public static IList<T> Bar<T>(this DataTable table) where T : class, new()
{
IList<T> list = ... // do something with DataTable.
return list;
}
}
I call this method like this:
DataTable table = SomehowGetTable();
IList<MyObject> list = table.Bar<MyObject>();
Again, greatly simplified.
Now, what I’d like to do, is add a delegate(?) so that after I get the list, I can call a method on each T in the list, like so:
public static class Foo
{
public static IList<T> Bar<T>(this DataTable table, MyDelegate postProcess)
{
IList<T> list = ... // do something with DataTable.
foreach(T item in list)
{
t.postProcess();
}
return list;
}
}
I don’t know what I need to be able to pass in postProcess.. a delegate, a Predicate, or an Action, mostly because I’m Linq-challenged still.
Or, maybe this isn’t even possible?
The signature would be
These days .Net brings virtually all method signatures to the table that you may ever need through
ActionandFuncdelegates. While action covers all void return type methods, Func introduces non-void returns.Note that T must be defined as type argument on your method. The compiler may be able to infer T from the action you provide into the method:
For example if you are actually processing the values coming into a different type, the signature may look like :
Used like