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

Include index operator #40071

Closed
BillWagner opened this issue Mar 18, 2024 · 3 comments · Fixed by #41655
Closed

Include index operator #40071

BillWagner opened this issue Mar 18, 2024 · 3 comments · Fixed by #41655
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc null-safety/subsvc Pri1 High priority, do before Pri2 and Pri3 resolved-by-customer Indicates issues where the customer no longer needs any help. [org][resolution] user-feedback Issues related to "Unified User Feedback".

Comments

@BillWagner
Copy link
Member

BillWagner commented Mar 18, 2024

Type of issue

Missing information

Description

The index operator [] isn't mentioned in this article. It should be noted that accessing an indexer could also throw an exception, just like the member access operator (.).

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/nullable-references.md

Document Version Independent Id

11f314de-dd3a-362e-b2bb-d0a2133cfc7b

Article author

@BillWagner

Metadata

  • ID: a690704d-da32-dca7-ea3e-66b84fb1d00d
  • Service: dotnet-csharp
  • Sub-service: null-safety
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Mar 18, 2024
@issues-automation issues-automation bot added dotnet-csharp/svc null-safety/subsvc Pri1 High priority, do before Pri2 and Pri3 labels Mar 18, 2024
@BillWagner BillWagner added the user-feedback Issues related to "Unified User Feedback". label Mar 18, 2024
@BillWagner BillWagner changed the title Include member access operator Include index operator Mar 19, 2024
@BillWagner BillWagner added the doc-enhancement Improve the current content [org][type][category] label Mar 19, 2024
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Mar 19, 2024
@BartoszKlonowski
Copy link
Contributor

@BillWagner Is this issue still valid? I'm looking at the very last section of that page, here:
https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references#arrays
and I see it describes the possibility of an exception thrown by [] operator.
If I get this wrong, then if you could elaborate a bit and assign me that would be great!

@BillWagner
Copy link
Member Author

Hi @BartoszKlonowski

Yes, it's still valid. The paragraph at the end describes the "hole" in analysis for arrays of a reference type:

string[] someNotes = new string[10];
Console.WriteLine(string[5].Length); // Array elements are null

What's missing is that a type with an indexer throws a NullReference Exception on [] just like on .:

public class C
{
    public void M() {}
    public this[int i]
    { 
        get { /* ... */ }
        set { /* ... */}
    }
}

// elsewhere:

C c = default;
c.M(); // Null Reference Exception
c[4] = 24; // Null Reference Exception. Needs to be descxribed.
int j = c[4]; // Null Reference Exception

Does that make sense?

@BartoszKlonowski
Copy link
Contributor

@BillWagner Yes, totally, thank you!
I will launch a PR with this addition.

@dotnet-bot dotnet-bot added the resolved-by-customer Indicates issues where the customer no longer needs any help. [org][resolution] label Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc null-safety/subsvc Pri1 High priority, do before Pri2 and Pri3 resolved-by-customer Indicates issues where the customer no longer needs any help. [org][resolution] user-feedback Issues related to "Unified User Feedback".
Projects
None yet
3 participants