Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda variable scope

Tags:

c#

lambda

scoping

Example:

myObject.Stub(s => s.MyMethod(null)).IgnoreArguments().Return("bleh");

var s = "s";

A variable "s" is defined in a lambda and another variable "s" as a local variable within the same method. Visual Studio tells me "A conflicting variable is defined below" when I hover over the first "s". Why are these conflicting; the "s" in the lambda is not available outside of its enclosing brackets surely?

like image 902
Ben Aston Avatar asked Jun 28 '10 15:06

Ben Aston


People also ask

What is the scope of a lambda?

1 Scope of a Lambda Expression. The body of a lambda expression has the same scope as a nested block. The same rules for name conflicts and shadowing apply. It is illegal to declare a parameter or a local variable in the lambda that has the same name as a local variable.

How are lambda expressions scoped?

Lexical scoping. Unlike inner classes, lambda expressions are lexically scoped, meaning that the body of a lambda expression are scoped just like a code block in the enclosing environment, with local variables for each formal parameter. The 'this' variable (and any associated OuterClassName.

Can a lambda be put in a variable?

Lambda expressions can use variables defined in an outer scope. We refer to these lambdas as capturing lambdas. They can capture static variables, instance variables, and local variables, but only local variables must be final or effectively final.

What is lambda variable?

An environment variable is a pair of strings that is stored in a function's version-specific configuration. The Lambda runtime makes environment variables available to your code and sets additional environment variables that contain information about the function and invocation request.


1 Answers

They are conflicting because a rule of C# is that any two uses of the same simple name cannot be used to refer to two different things inside the block immediately enclosing either of them. In your example the simple name "s" is used to mean two things inside the block enclosing the local variable declaration: it means a local variable, and a lambda parameter. That is what is illegal. I note that the error message you get tells you this:

A local variable named 's' cannot be declared in this scope because it
would give a different meaning to 's', which is already used in a 
'child' scope to denote something else

C# does not allow you to have the same simple name mean two things in the same block because doing so makes code error prone, hard to edit, hard to read, hard to refactor, and hard to debug. It is better to disallow this bad programming practice than to allow it and risk you causing bugs because you assumed that "s" means the same thing throughout the block.

When the code is only two lines long it is easy to remember that there are two different meanings for s, but when it is hundreds of lines long, not so easy.

For more information about this rule, see:

http://blogs.msdn.com/b/ericlippert/archive/2009/11/02/simple-names-are-not-so-simple.aspx

like image 192
Eric Lippert Avatar answered Oct 06 '22 08:10

Eric Lippert