Compiler Error CS1628
TOC
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Compiler Error CS1628

Cannot use ref or out parameter 'parameter' inside an anonymous method, lambda expression, or query expression

This error occurs if you use a ref or out parameter inside an anonymous method block. To avoid this error, use a local variable or some other construct.

The following sample generates CS1628:

// CS1628.cs

delegate int MyDelegate();

class C
{
  public static void F(ref int i)
  {
      MyDelegate d = delegate { return i; };  // CS1628
      // Try this instead:
      // int tmp = i;
      // MyDelegate d = delegate { return tmp; };
  }

  public static void Main()
  {
     
  }
}
Show:
© 2016 Microsoft