Skip to content

Another cause for CS1677 (and CS1661) #43779

Open
@RexJaeschke

Description

@RexJaeschke

Type of issue

Missing information

Description

I'm working on the Ecma C# spec with lambda/anonymous function parameters [where I'm adding scoped], and my tests generated two CS errors, CS1677 and CS1661, from the same code. While I'll document the issue here, it likely will require tweaking the description for CS1661 as well.

Basically, the explanation and example you currently provide does not handle my situation.

Consider the following:

Action<int> lam3a = (in int i) => Mz2(in i);   // CS1677 on `i` in `in int i`, and CS1661 on `=>`
        var lam3b = (in int i) => Mz2(in i);   // no errors, and call below works fine.
int xx = 123;
lam3b(in xx);

public static void Mz2(in int v) { Console.WriteLine("In Mz2"); }

The CS1677 message I get is

Parameter 1 should not be declared with the 'in' keyword

The CS1661 message I get is

Cannot convert lambda expression to type 'Action<int>' because the parameter types do not match the delegate parameter types.

It appears that the explicit declaration type of lam3a is the problem, yet I don't see any way to change that (you can't include in in the type parameter), but when I let the compiler figure out the delegate type by using var, it works. This suggests that var can allow code that cannot be expressed directly using explicit types.

It seems there are multiple scenarios that can result in CS1661, as the message I got in the test above does not match the one in your Help entry, which states,

Cannot convert anonymous method block to delegate type 'delegate type' because the specified block's parameter types do not match the delegate parameter types

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1677?f1url=%3FappId%3Droslyn%26k%3Dk(CS1677)

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/misc/cs1677.md

Document Version Independent Id

9f888ae8-1183-8c38-f6d2-0dc075026016

Article author

@BillWagner

Metadata

  • ID: 802df120-d60d-3838-0ad6-e1437197950a
  • Service: dotnet-csharp
  • Sub-service: errors-warnings

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions