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

The implicit index access example seemed not working #42674

Closed
WeihanLi opened this issue Sep 21, 2024 · 1 comment
Closed

The implicit index access example seemed not working #42674

WeihanLi opened this issue Sep 21, 2024 · 1 comment

Comments

@WeihanLi
Copy link
Contributor

WeihanLi commented Sep 21, 2024

Type of issue

Code doesn't work

Description

The implicit index access example seemed not working

image

int[] numbers =
{
   [^1] = 4,
   [^2] = 3,
   [^3] = 2,
   [^4] = 1,
};

CS0131: The left-hand side of an assignment must be a variable, property or indexer

And I'm using the latest vs preview and targeted net9.0 with .NET 9 RC1 SDK installed

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#implicit-index-access

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/whats-new/csharp-13.md

Document Version Independent Id

587ed47f-f532-e018-9b0d-6ee76afd20e4

Article author

@BillWagner

Metadata

  • ID: c36588d0-6bdf-1740-220c-10b20bb88730
  • Service: dotnet-csharp
  • Sub-service: whats-new
@WeihanLi
Copy link
Contributor Author

The array needs to be inside of an object, there's an example

internal class ImplicitIndexAccessSample
{
    public static void Main()
    {
        var a = new TestClass()
        {
            Numbers =
            {
                [0] = 3,
                [^1] = 1
            }
        };
        foreach (var item in a.Numbers)
        {
            Console.WriteLine(item);
        }

        // output 3 0 1
    }
}

file sealed class TestClass
{
    public int[] Numbers { get; init; } = new int[3];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants