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

Struct initialization and default values – definitive assignment place #42331

Open
bravequickcleverfibreyarn opened this issue Aug 26, 2024 · 0 comments · May be fixed by #43694
Open

Struct initialization and default values – definitive assignment place #42331

bravequickcleverfibreyarn opened this issue Aug 26, 2024 · 0 comments · May be fixed by #43694
Assignees
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc in-pr This issue will be closed (fixed) by an active pull request. lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3

Comments

@bravequickcleverfibreyarn
Copy link
Contributor

Type of issue

Typo

Description

From https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct#struct-initialization-and-default-values

The compiler performs its usual definite assignment analysis. Any fields that are accessed before being assigned, or not definitely assigned when the constructor finishes executing are assigned their default values before the constructor body executes.

This phrase is little confusing. Having example

internal struct FieldInitializationTestStruct
{
    public int x;
    public int y = 1;
    public int z;

    public FieldInitializationTestStruct()
    {
        var f = x+2;
        z = x;
    }
}

MSIL produced for .ctor body is

IL_0000: ldarg.0
IL_0001: ldc.i4.0
IL_0002: stfld int32 LangEnvTests.FieldInitializationTestStruct::x // intialized here
IL_0007: ldarg.0
IL_0008: ldc.i4.1
IL_0009: stfld int32 LangEnvTests.FieldInitializationTestStruct::y
IL_000e: ldarg.0
IL_000f: ldarg.0
IL_0010: ldfld int32 LangEnvTests.FieldInitializationTestStruct::x
IL_0015: stfld int32 LangEnvTests.FieldInitializationTestStruct::z
IL_001a: ret

so field is initialized before access but in constructor body.

So, IMHO:

  • Having default struct, all fields are initialized by default to 0-bit pattern via initobj.
  • Having new struct, all fields are initialized as part of ctor run where field and default initializations and are place to .ctor by compilation.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct#struct-initialization-and-default-values

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/builtin-types/struct.md

Document Version Independent Id

0960802e-80c3-cb56-cb8b-ab1f1b20fb4e

Article author

@BillWagner

Metadata

  • ID: d9220116-e950-04cf-b6d5-70d15a084a22
  • Service: dotnet-csharp
  • Sub-service: lang-reference
@issues-automation issues-automation bot added dotnet-csharp/svc lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3 labels Aug 26, 2024
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Aug 26, 2024
@dotnet dotnet deleted a comment Aug 26, 2024
@dotnet dotnet deleted a comment Aug 26, 2024
@dotnet dotnet deleted a comment Aug 26, 2024
@dotnet dotnet deleted a comment Aug 26, 2024
@BillWagner BillWagner added the doc-enhancement Improve the current content [org][type][category] label Aug 26, 2024
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Aug 26, 2024
@BillWagner BillWagner self-assigned this Nov 22, 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 22, 2024
@BillWagner BillWagner linked a pull request Nov 22, 2024 that will close this issue
@dotnet-policy-service dotnet-policy-service bot added the in-pr This issue will be closed (fixed) by an active pull request. label Nov 22, 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 22, 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 in-pr This issue will be closed (fixed) by an active pull request. lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3
Projects
Status: 👀 In review
Development

Successfully merging a pull request may close this issue.

3 participants