0

I am running Visual Studio 2015 Enterprise, .NET 4.6, and I wrote some code that relies on Delegates being static. I ran the exact code from this question, but I get "false":

static void Main(string[] args)
{
    Action<string> actionMethod = s => { Console.WriteLine("My Name is " + s); };

    // Always false
    Console.WriteLine(actionMethod.Method.IsStatic);

    Console.Read();
}

Is this a bug? Or does C# not guarantee that lambdas without nonlocal dependencies are made static?

Is there a workaround to determine if a lambda/delegate has nonlocal dependencies? My goal is to know if a lambda caught it's enclosing variables / uses a closure.

1

1 Answer 1

2

Or does C# not guarantee that lambdas without nonlocal dependencies are made static?

It makes no such guarantee. That was an implementation detail of some versions of the compiler; one that it has no obligation to continue with going forward. As you have seen, they've changed that implementation detail.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.