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

Arrays that contain reference types #21941

Closed
matt-rule opened this issue Dec 10, 2020 · 2 comments · Fixed by #43914
Closed

Arrays that contain reference types #21941

matt-rule opened this issue Dec 10, 2020 · 2 comments · Fixed by #43914
Assignees
Labels
dotnet-csharp/svc in-pr This issue will be closed (fixed) by an active pull request. null-safety/subsvc okr-freshness OKR: Freshness of content okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. 📌 seQUESTered Identifies that an issue has been imported into Quest.

Comments

@matt-rule
Copy link

matt-rule commented Dec 10, 2020

I Google searched for "arrays of nullable reference types c#" because there are some interesting design decisions in this area of .NET. I found this very informative article on the pitfalls of using nullable reference types. One of these pitfalls is arrays of non-nullable references. While the article is informative in general, there is only a paragraph of information about this particular topic and I feel like there could be more info because I'm currently working with these in my project and trying to work out what options there are.

My request isn't in the exact format you asked for, because I'm not asking for a whole article to be written on this. It would be nice to see a paragraph or two added to the existing article, discussing:

  • Why arrays of non-nullable references are initialised to null.
  • Why the compiler lets the programmer create an array of non-nullable references without a warning.
  • What options you can take / recommended practices to avoid potential issues resulting from non-nullable references being set to null unexpectedly.

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.


Associated WorkItem - 350756

@BillWagner
Copy link
Member

BillWagner commented Dec 10, 2020

That's a great suggestion @matt-rule

I'll answer what I can briefly here, and we'll add this article to our next set of updates on null safety.

  • Why arrays of non-nullable references are initialized to null.

There isn't a good alternative. Perf is one reason: all those objects would need to be allocated. All those constructors would need to run. It would also require an accessible default constructor.

  • Why the compiler lets the programmer create an array of non-nullable references without a warning.

Partially the first answer: there isn't a good way to fix that warning.

  • What options you can take / recommended practices to avoid potential issues resulting from non-nullable references being set to null unexpectedly.

These are harder. The short version is to avoid the visibility of the array before objects in the array are allocated or initialized.

I know those answers are incomplete. I hope that helps you get started while we address this with a new article.

@matt-rule
Copy link
Author

Thank you very much Bill! I know this is off-topic but coincidentally yesterday I bought your book "More Effective C#" from Amazon. Looking forward to a good read!

@gewarren gewarren added Pri1 and removed P1 labels Jan 29, 2021
@tdykstra tdykstra self-assigned this May 7, 2021
@BillWagner BillWagner assigned BillWagner and unassigned tdykstra Nov 28, 2024
@dotnetrepoman dotnetrepoman bot added 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. and removed 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. labels Nov 28, 2024
@BillWagner BillWagner added the 🗺️ reQUEST Triggers an issue to be imported into Quest. label Nov 28, 2024
@dotnetrepoman dotnetrepoman bot added 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. and removed 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. labels Dec 2, 2024
@sequestor sequestor bot added 📌 seQUESTered Identifies that an issue has been imported into Quest. and removed 🗺️ reQUEST Triggers an issue to be imported into Quest. labels Dec 3, 2024
@BillWagner BillWagner added okr-freshness OKR: Freshness of content okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. labels Dec 4, 2024
@BillWagner BillWagner moved this from 🔖 Ready to 🏗 In progress in dotnet/docs December 2024 Sprint Dec 5, 2024
@dotnetrepoman dotnetrepoman bot added 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. and removed 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. labels Dec 5, 2024
BillWagner added a commit to BillWagner/docs that referenced this issue Dec 9, 2024
Fixes dotnet#21941: Add description of the default values for arrays of reference types.
Fixes dotnet#30348: Edit pass
Fixes dotnet#33667:  Update the description of the nullable context to match the language used in the C# 8 draft standard
Fixes dotnet#36767: Add language on declaring a non-null array of nullable reference types and a nullable array of non-nullable reference types.

Also, perform a general edit pass.
@dotnetrepoman dotnetrepoman bot added the 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. label Dec 9, 2024
@BillWagner BillWagner moved this from 🏗 In progress to 👀 In review in dotnet/docs December 2024 Sprint Dec 9, 2024
@dotnetrepoman dotnetrepoman bot removed the 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. label Dec 9, 2024
@dotnet-policy-service dotnet-policy-service bot added in-pr This issue will be closed (fixed) by an active pull request. labels Dec 9, 2024
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in dotnet/docs December 2024 Sprint Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc in-pr This issue will be closed (fixed) by an active pull request. null-safety/subsvc okr-freshness OKR: Freshness of content okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. 📌 seQUESTered Identifies that an issue has been imported into Quest.
Projects
No open projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

6 participants