Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the following code compile without errors?

Tags:

I was messing around with my C# project a little bit and I was surprised to see this code compiles:

var a = new Action<string>(ref b => b = "hello");

Flipping it the other way around, like Action<string> a = ref b => b = "hello"; will give a syntactical error at 'ref'.

Sure enough, copy-pasting the snippet into http://dotnetfiddle.net gives a compiler error:

A ref or out argument must be an assignable variable

Have I encountered a bug in the compiler? (I'm using VS 2015 and C# 6.)


EDIT: Just switched to the Roslyn compiler on .NET Fiddle, it still emits the same error.

like image 929
James Ko Avatar asked Nov 08 '15 16:11

James Ko


1 Answers

It sounds like a compiler bug in VS2015. This is filed as https://github.com/dotnet/roslyn/issues/6646

like image 98
Neal Gafter Avatar answered Nov 20 '22 17:11

Neal Gafter