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

Default value behaviour – nullable value types point in list is inconsistent with other places and incorrect #27181

Closed
bravequickcleverfibreyarn opened this issue Nov 19, 2021 · 2 comments · Fixed by #36782
Assignees
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc fundamentals/subsvc Pri2 📌 seQUESTered Identifies that an issue has been imported into Quest.

Comments

@bravequickcleverfibreyarn
Copy link
Contributor

bravequickcleverfibreyarn commented Nov 19, 2021

  • For value types, the array elements are initialized with the default value, the 0-bit pattern; the elements will have the value 0.
  • All the reference types (including the non-nullable), have the values null.
  • For nullable value types, HasValue is set to false and the elements would be set to null.

From Types|Default constructors

For a nullable_type the default value is an instance for which the HasValue property is false and the Value property is undefined. The default value is also known as the null value of the nullable type.

From Default values of C# types (C# reference)

Any nullable value type | An instance for which the HasValue property is false and the Value property is undefined. That default value is also known as the null value of a nullable value type.


FPOV is it correct to state that value hidden in Nullable<T> is default (not null) rather than presenting behavior characteristics of Value property. That since initialization/default is discussed which by nature relates to fields.

On the other hand private readonly bool hasValue stays in darkness of black box for consumer so there is little antinomy in such approach.

Maybe just right would be to state 0-bit pattern obedience along with behavior of Value property. Many other structs are black boxes that return via properties so I guess there is no need to tell explicitly that after default initialization any bool property returns false.


Document Details

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


Associated WorkItem - 148959

@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Nov 19, 2021
@bravequickcleverfibreyarn bravequickcleverfibreyarn changed the title Default value behaviour – nullable value types point in list is inconsistent with other places Default value behaviour – nullable value types point in list is inconsistent with other places and incorrect Nov 19, 2021
@BillWagner BillWagner added the doc-enhancement Improve the current content [org][type][category] label Nov 23, 2021
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Nov 23, 2021
@BillWagner
Copy link
Member

Thanks for noting this @Uzivatel919

I want to think about how to present this in a way that is both consistent and usable. There are a few related behaviors with respect to arrays and types (especially nullable types). I think the updates are somewhere along these lines:

  • When an array is constructed, the storage for all the elements is initialized to the 0 bit pattern. That means:
    • All struct types are initialized to their 0 values. (Note that in C# 10, once structs can have a default constructor, that may be different than the instance generated from the default constructor.)
    • All reference types are initialized to null. This includes non-nullable value types. An array of a non-nullable reference type has all null elements.
    • Nullable value types are initialized to the 0 bit pattern. That means HasValue is false, and Value is undefined.

The reason for this behavior is to balance the performance needs of not calling a parameterless constructor for each element with having well defined behavior for freshly allocated arrays.

@PRMerger15 PRMerger15 added the Pri2 label Dec 1, 2021
@BillWagner BillWagner self-assigned this Aug 21, 2023
@BillWagner BillWagner added the 🗺️ reQUEST Triggers an issue to be imported into Quest. label Aug 21, 2023
@BillWagner
Copy link
Member

Fixing this as part of the work on collection expressions. #36356

@BillWagner BillWagner moved this from 🔖 Ready to 🏗 In progress in dotnet/docs August 2023 sprint Aug 21, 2023
@github-actions github-actions bot added 📌 seQUESTered Identifies that an issue has been imported into Quest. and removed 🗺️ reQUEST Triggers an issue to be imported into Quest. labels Aug 22, 2023
BillWagner added a commit to BillWagner/docs that referenced this issue Aug 25, 2023
Edit and update the Arrays article. Remove older outdated samples, update style (both text and code), and fix two open issues:

- Fix dotnet#27181 Clarify the default value for array elements.
- Fix dotnet#29181 Clarify the row, column for a 2D array.
@ghost ghost added the in-pr This issue will be closed (fixed) by an active pull request. label Aug 25, 2023
@BillWagner BillWagner moved this from 🏗 In progress to 👀 In review in dotnet/docs August 2023 sprint Aug 29, 2023
BillWagner added a commit to BillWagner/docs that referenced this issue Aug 31, 2023
Edit and update the Arrays article. Remove older outdated samples, update style (both text and code), and fix two open issues:

- Fix dotnet#27181 Clarify the default value for array elements.
- Fix dotnet#29181 Clarify the row, column for a 2D array.
BillWagner added a commit that referenced this issue Aug 31, 2023
* Update TOC

Move programming guide articles on arrays and collections into the language reference section. This set of content will be updated and edited, then collection expressions will be added.

* Move array content

Move all array content into the language reference.

* Move all snippets

Move snippets to the language reference location.

* Update internal links

Run cleanrepo to update redirected links with the new target, and replace absolute links with relative links.

* Edit pass on collections article

Fix open issues on the collection articles, and perform an edit pass to update.

Simplify examples and remove outdated examples. Update text. Instead of listing all collections, refer to articles in the .NET fundamentals and API reference where all collections are listed.

* Edit pass on Arrays article

Edit and update the Arrays article. Remove older outdated samples, update style (both text and code), and fix two open issues:

- Fix #27181 Clarify the default value for array elements.
- Fix #29181 Clarify the row, column for a 2D array.

* rough draft of collection expressions.

* grammar check and build issues

Do a grammar check on the new article, fix build warnings, and add links.

* modify title

The language reference and spec titles were duplicated

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>

* respond to feedback.

---------

Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in dotnet/docs August 2023 sprint Aug 31, 2023
@ghost ghost removed the in-pr This issue will be closed (fixed) by an active pull request. label Aug 31, 2023
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 fundamentals/subsvc Pri2 📌 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.

5 participants