Sachin Kainth
Sachin Kainth

Reputation: 46740

Writing a very generic method

I have a method like this

void Foo(IMyInterface obj)
{

}

Now, sometimes in this method I want to do this

obj.A++;

and sometimes I want to do this

obj.B--;

The question is, how can I pass something into this method to allow this sort of thing in a generic way so that no matter what I pass in, it will get executed. So, I'm looking for an Expression or a Func or something like that.

Upvotes: 0

Views: 72

Answers (1)

It'sNotALie.
It'sNotALie.

Reputation: 22794

You're looking for an Action<T>:

static void Foo<T>(this T obj, Action<T> action) where T : IMyInterface
{
    action(obj);
}

Upvotes: 3

Related Questions