Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target-typed new can be passed in implicit-typed array #3796

Closed
KyouyamaKazusa0805 opened this issue Aug 15, 2020 · 2 comments
Closed

Target-typed new can be passed in implicit-typed array #3796

KyouyamaKazusa0805 opened this issue Aug 15, 2020 · 2 comments

Comments

@KyouyamaKazusa0805
Copy link

KyouyamaKazusa0805 commented Aug 15, 2020

Consider the code in C# 9:

public class C
{
    public void M()
    {
        A[] z1 = new[] { new() }; // CS0826.
        var z2 = new[] { new A() };
        var z3 = new A[] { new() };
        var z4 = new A[] { new A() };
    }
}

public readonly struct A
{
    public readonly int Prop => 3;
}

I think that all four cases should be okay, but in fact, Roslyn throws a syntax error CS0826 (No best type found for implicitly-typed array) for the new expression of z1 . The reason why I think z1 is okay is that A is a struct, so new[] means new A[] as the only possibility. Therefore, the inner expression new() should stand for new A().

@CyrusNajmabadi
Copy link
Member

We could consider target-typing implicit arrays as a fallback if the type of it cannot be determined based on its elements.

@333fred
Copy link
Member

333fred commented Aug 15, 2020

Duplicate of #2701. I'm going to close this out.

@333fred 333fred closed this as completed Aug 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants