I have a solution with a bunch of projects, all running on .Net Standard 2.0 or .Net Core 2.2. Also due to historic reason in all project files the language version is defined as <LangVersion>latest<LangVersion>
. Since one of the last Visual Studio updates (currently I'm on 16.3.7) also .Net Core 3.0 and C# 8 came available. So far so good.
Then as time passed by I implemented some functionality based on EF core (but that doesn't matter in this case) and wrote the following code (more or less):
var entity = _context.SomeEntities
.Where(entity => entity.Id == id)
.FirstOrDefault();
And it compiled without any problems! Where is the error message A local or parameter named 'item' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
?
Even the following code compiles without any errors:
var items = Enumerable.Range(1, 10);
var item = 2;
Console.WriteLine(item);
item = items.FirstOrDefault(item => item % 2 == 0);
I would guess the compiler picks the nearest variable and does what the programmer hopefully intended. Unfortunately I didn't find any information about that this is one of the new features of C# 8 and is allowed now. But I found some other (and older) posts like this discussion that it is better to let the compiler fail in these cases, cause it makes more trouble in the long shot and now this feature is simply gone.
So does anyone have some links or documentation about this specific new behaviour and if this is really intended and what are the reasons for the mind change?