Skip to content

16.4.10 Highlight differences with static constructors? #132

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

Open
jskeet opened this issue Feb 10, 2016 · 6 comments
Open

16.4.10 Highlight differences with static constructors? #132

jskeet opened this issue Feb 10, 2016 · 6 comments
Labels
type: clarity While not technically incorrect, the Standard is potentially confusing

Comments

@jskeet
Copy link
Contributor

jskeet commented Feb 10, 2016

Static constructors for structs follow most of the same rules as for classes.

That makes one wonder what rules it doesn't follow. I think we should elaborate.

Additionally:

  • It would be good to reference 15.12 at the end of the above sentence
  • This subsection isn't referenced from 16.4.1, unlike almost all other subsections.
@Nigel-Ecma
Copy link
Contributor

Nigel-Ecma commented Feb 16, 2016

Agreed in principle. Someone should determine in what way the differ, if they do, and wordsmith a suitable §16.4.10; and add it to the list in §16.4.1 - I expect the latter is just an oversight; and make this a Proposal.

@jonpryor
Copy link

jonpryor commented Feb 16, 2016

The entirety of §16.4.10 is weird. It begins:

Static constructors for structs follow most of the same rules as for classes.

Should this not provide a link to §15.12?

Ignoring that, the section then proceeds:

The execution of a static constructor for a struct type is triggered by the first of the following events to occur within an application domain:

  • An explicitly declared constructor of the struct type is called.

This sounds like it's providing an exhaustive list, but it's at minimum missing use of a static member; cribbing from §15.12:

  • Any of the static members of the class type are referenced.

I also don't understand why this bullet point was removed:

An instance member of the struct is referenced.

The static constructor appears to be executed before an instance member is referenced, at least in this example:

using System;
struct Point {
    static Point ()
    {
        Console.WriteLine ("# Point..cctor");
    }

    int x;
    public int X {
        get {return x;}
        set {x = value;}
    }
    public Point(int x, int y) {
        this.x = x;
    }
}

class App {
    public static void Main ()
    {
        Console.WriteLine ("Start.");
        var p = new Point ();
        Console.WriteLine ("p.X={0}", p.X);
        Console.WriteLine ("Done.");
    }
}

Program output with mono 4.3.2:

Start.
# Point..cctor
p.X=0
Done.

Removing the access to p.X also removes the execution of the static constructor.

@jskeet jskeet changed the title 17.4.11 Highlight differences with static constructors? 16.4.11 Highlight differences with static constructors? Mar 26, 2018
@jskeet
Copy link
Contributor Author

jskeet commented Mar 26, 2018

All comments prior to this one were before we removed section 7. I've updated the title only.

@Nigel-Ecma Nigel-Ecma changed the title 16.4.11 Highlight differences with static constructors? 16.4.10 Highlight differences with static constructors? Sep 14, 2020
@Nigel-Ecma
Copy link
Contributor

Clause numbers in all comments above updated, it was getting confusing!

@Nigel-Ecma
Copy link
Contributor

What the semantics are here needs to be resolved, it shouldn't be punted again. I have a vague recollection that this was a discussion area for the CLI and it's semantics may have changed as a result – which I expect impacts the C# compilers, and in turn this Standard... Labelling appropriately

@jskeet jskeet transferred this issue from another repository Jan 8, 2021
@jskeet
Copy link
Contributor Author

jskeet commented Jan 8, 2021

Related: #125

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: clarity While not technically incorrect, the Standard is potentially confusing
Projects
None yet
Development

No branches or pull requests

3 participants