dotnet / dotnet/docs

Another cause for CS1677 (and CS1661)

Open
#43,779 0 comments 0 reactions 0 assignees View on GitHub
dotnet-csharp/svc errors-warnings/subsvc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

### 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:

```charp
Action 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\' 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](https://github.com/dotnet/docs/issues?q=is%3Aissue+is%3Aopen+9f888ae8-1183-8c38-f6d2-0dc075026016)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.