Skip to content

Extra info for CS1061 #43889

@RexJaeschke

Description

@RexJaeschke

Type of issue

Other (describe below)

Description

While writing tests for the Ecma C# spec, I got this error in a context where the method is being called by compiler-generated code rather than by my program, and I thought it might be worth mentioning here.

Consider the following:

MyType q1 = new MyType { 1, 1.2, "abc" };

class MyType : IEnumerable
{
    public System.Collections.IEnumerator GetEnumerator()
    {
        return (IEnumerator)GetEnumerator();
    }
}

I get this error 3 times, once for each expression in the collection initializer, { 1, 1.2, "abc" }. The reason is that for each expression, the compiler generates a call to Add(type expression), and in my case, type MyType does not have any such method, and one or more (in this case, 3) need to be added. So, while that is covered by your existing explanation, the reason for it is not at all obvious to the new programmer.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1061?f1url=%3FappId%3Droslyn%26k%3Dk(CS1061)

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/compiler-messages/cs1061.md

Document Version Independent Id

df4263c1-477a-3c32-9abe-3fcb850e6c1b

Article author

@BillWagner

Metadata

  • ID: 60c946d7-649b-44e4-49a9-543565904d18
  • 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