Calling a Delegate of an instance method on a Generic?

The signature would be public static IList Bar(this DataTable table, Action postProcess) { var list = // Get ILIst from DataTable foreach (var I in list) postProcess(i); } These days . Net brings virtually all method signatures to the table that you may ever need through Action and Func delegates. 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: List myDoubles = table. Bar((double x) => Debug. Writeline(x)) For example if you are actually processing the values coming into a different type, the signature may look like : public static IList Bar(this DataTable table, Func postProcess) { return /* Get Listof T */ .

Select(postProcess).ToList(); } Used like List values = table. Bar((double d) => (int)d).

The signature would be public static IList Bar(this DataTable table, Action postProcess) { var list = // Get ILIst from DataTable foreach (var I in list) postProcess(i); } These days . Net brings virtually all method signatures to the table that you may ever need through Action and Func delegates. 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: List myDoubles = table. Bar((double x) => Debug.

Writeline(x)); For example if you are actually processing the values coming into a different type, the signature may look like : public static IList Bar(this DataTable table, Func postProcess) { return /* Get Listof T */ . Select(postProcess).ToList(); } Used like List values = table. Bar((double d) => (int)d).

I'm accepting this one as it was technically first according to stackoverflow, and also for providing a sample to call it with. This is exactly what I was looking for. – Moose Mar 25 at 15:03.

What you want is an Action so you can do: public static class Foo { public static IList Bar(this DataTable table, Action postProcess) { IList list = ... // do something with DataTable. Foreach(T item in list) { postProcess(item); } return list; } }.

T is the generic type of the items in your IList. Am I not understanding you correctly? – jjrdk Mar 25 at 15:38 The list is the return Type.

See Adam's answer. – Henk Holterman Mar 25 at 16:27.

First, you need to define Bar as generic, otherwise it won't compile. Second, if you're trying to operate on each element within the list, you need to pass in a delegate that takes a single parameter of type T and returns no value. A built-in .

NET delegate type is Action, and it'll do fine. So, public static IList Bar(this DataTable table, Action postProcess) { IList list = ... // do something with DataTable foreach(T item in list) { postProcess(item); } return list; }.

You can do either public static IList Bar(this DataTable table, Action postProcess) { ... postProcess(someT); ... } or add a generic constraint: public static IList Bar(this DataTable table) where T : IHasPostProcess { ... someT.postProcess(); ... }.

1 I think you mean T : IHasPostProcess – Adam Robinson Mar 25 at 14:21.

Try Action: public static class Foo { public static IList Bar(this DataTable table, Action postProcess) { IList list = ... // do something with DataTable. Foreach(T item in list) { postProcess(item); } return list; } } Or use an interface so you do not have to pass action: (I prefer this one) public interface IDo { Do(); } public static class Foo { public static IList Bar(this DataTable table) where T : IDo { IList list = ... // do something with DataTable. Foreach(T item in list) { item.Do(); } return list; } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions