tags:

views:

36

answers:

3

As the title says. Where do they go? they're variables, but they're also code...

+1  A: 

They should go where they're needed. Can you clarify your question?

A variable that holds a reference to a lambda (or any Delegate) is still a variable. Treat it like a variable.

That doesn't mean you HAVE to use variables. You can just specify the lambda inline in many cases.

John Weldon
what is unclear?
RCIX
do you mean where should I declare the variables that hold the reference to the lambda? if so: with the variables.
John Weldon
See my example.
RCIX
A: 

I'd put them with the rest of your variables, since they can be reassigned and changed just like any other variable. Like this:

class Test
{
    string s = "abcdefg";
    int one = 1;
    Func<int> myFunc;

    void MyMethod()
    {
        int x = 5;
        float f = 3.86;
        Action<string> a;
    }
}

I'm not quite sure what else (or where else) you would mean?

jasonh
ok, but what about when one is predefined?
RCIX
A: 

An example:

private Func<bool> isFooOn = () =>
{
    //do something
};
RCIX
I think this is personal taste really. If you plan to reassign them, you may want to put them with variables. On the other hand, it may be cleaner to put them with the rest of your code, especially if you use region tags to group your code.
jasonh